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

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.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

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

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.

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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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