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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

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.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.