How to generate scatter plot using Pandas and Seaborn?

This recipe helps you generate scatter plot using Pandas and Seaborn

Recipe Objective

Have you ever feel a need to visualize the data in various form. Visualizing the data give us a better idea how our dataset is distributed. We can plot the data and draw a best fitted regression line using Seaborn.

This data science python source code does the following :
1. Importing necessary libraries for making plot
2. Sets style of the scatter plot
3. Plots without regression line
4. Plots by fitting regession line

So this is the recipe on how we can generate scatter plot using Pandas and Seaborn.

Step 1 - Import the library

import pandas as pd import random import matplotlib.pyplot as plt import seaborn as sns

We have imported various modules like pandas, random, matplotlib and seaborn which will be need for the dataset.

Step 2 - Setting up the Data

We have created a empty dataset and then by using random function we have created set of random data and stored in X and Y. We have used print function to print the first five rows of dataset. df = pd.DataFrame() df['x'] = random.sample(range(1, 500), 70) df['y'] = random.sample(range(1, 500), 70) print(df.head())

Step 3 - Ploting Scatterplot without Regression line

First we are ploting scatterplot without regression line, we are using sns.lmplot to plot the scatter plot. In the parameters we have passed data x, target y, dataframe, fit_reg as False because we dont want to get a regression line and in scatter_kws the values to set for the plot. We have also set the title, x and y axis labels. sns.lmplot('x', 'y', data=df, fit_reg=False, scatter_kws={"marker": "D", "s": 20}) plt.title('Scatter Plot of Data without Regression Line') plt.xlabel('X Axis') plt.ylabel('Y Axis') plt.show()

Step 4 - Ploting Scatterplot with Regression line

To plot scatterplot with regression line we have to just change fir_reg parameter as True. This will plot the scatterplot with a regression line. We have also set the title, x and y axis labels. sns.lmplot('x', 'y', data=df, fit_reg=True, scatter_kws={"marker": "D", "s": 20}) plt.title('Scatter Plot of Data with Regression Line') plt.xlabel('X Axis') plt.ylabel('Y Axis') plt.show() So the output comes as:

     x    y
0  247  493
1   38   71
2  352  142
3  239  173
4  266  453

Download Materials

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python