How to Draw a Scatter Plot Python

scatter plot python

How to Draw a Scatter Plot Python : In this tutorial we will see how to draw a scatter plot.

Overview

The Matplotlib and Seaborn libraries have a built-in function to create a scatter plot python graph called scatter() and scatterplot() respectively. This type of graph is often used to plot data points on the vertical and horizontal axes. Its purpose is to visualize that one variable is correlated with another variable. Each line of the dataset is represented by a point whose position corresponds to the value of the columns defined on the X and Y axes of the graph. We will therefore see how to set up this type of graph using the Matplotlib and Seaborn libraries.

Matplotlib Scatter Plot

The first solution is to use the matplotlib library. This library contains a scatter() function which allows to draw a scatter plot.

Here is an example of how to draw this type of graphic :

import matplotlib.pyplot as plt

revenue1 = [20, 25, 27, 50, 70, 70, 65, 35, 30, 15, 20, 24]
revenue2 = [30, 60, 70, 85, 44, 48, 30, 12, 16, 19, 23, 27]
month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
fig=plt.figure()
plt.scatter(x=month, y=revenue1, color='green')
plt.scatter(x=month, y=revenue2, color='blue')
plt.xlabel('Month')
plt.ylabel('Revenue')
plt.title('scatter plot example')

plt.show()
 

Here is the rendering of the code below:

scatter plot python matplotlib

In the code we have defined 3 parameters in the scatter() function:

  • x : corresponds to the column that will be defined on the X-axis in the graph.
  • y : corresponds to the column that will be defined as the Y-axis in the graph.
  • color : The color used for plotting the graph points

The function contains many parameters that are listed in the official matplotlib documentation:

https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.scatter.html

Seaborn Scatter Plot

The Seaborn library also allows you to make scatterplot graphs. From my point of view, I prefer to use this library which I find more attractive.

To use scatterplot, we need to use the scatterplot() function.

We will resume our example on the revenue generated per month. Here is what it looks like in the Seaborn version:

import seaborn as sns; 
sns.set()
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
print(iris)
ax = sns.scatterplot(x="sepal_length", y="sepal_width", data=iris)
plt.show()
 

We used the Iris dataset which is available in the Seaborn bookstore. You can get more information about this dataset at this link.

Output :

scatter plot python seaborn

If you want to have one color per flower species you can use the “hue” parameter of the scatterplot function:

import seaborn as sns;
sns.set()
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
print(iris)
ax = sns.scatterplot(x="sepal_length", y="sepal_width", hue="species", data=iris)
plt.show()
 

Output :

scatter plot python seaborn hue parameter

To have more details about the scatterplot() function you can find via this link the list of parameters you can use.

Summary

  • Using matplotlib, the function called is scatter()
  • Using Seaborn, the function called is scatterplot()

I hope this tutorial will allow you to create beautiful scatterplot graphics. Feel free to like or comment this article, it’s always nice to know that your article can help people 🙂

If you want to learn more about creating python graphics, you can read more about this book (As an Amazon Partner, I make a profit on qualifying purchases) :

See you soon 🙂

Main menu

Published
Categorized as Python

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Leave a comment

Your email address will not be published.