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

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.

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.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

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.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

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

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.