How to generate scatter plot using Pandas and Seaborn?
DATA VISUALIZATION DATA CLEANING PYTHON DATA MUNGING MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to generate scatter plot using Pandas and Seaborn?

How to generate scatter plot using Pandas and Seaborn?

This recipe helps you generate scatter plot using Pandas and Seaborn

0

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

Relevant Projects

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.