How to Use the Matplotlib.pyplot.scatter() Function in Python

09/09/2021

Contents

In this article, you will learn how to use the matplotlib.pyplot.scatter() function in Python.

Matplotlib.pyplot.scatter() Function

The scatter() function in the pyplot module of the Matplotlib library is used for plotting scatter plots in Python. A scatter plot is a type of plot that displays the relationship between two variables on two axes.

Here’s a basic example of how to use the scatter() function:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.scatter(x, y)
plt.show()

In this example, x and y are lists that represent the x and y coordinates of the data points to be plotted. The scatter() function takes these two lists as arguments and creates a scatter plot with the data points represented by individual markers.

You can also customize various aspects of the scatter plot, such as the size, color, and shape of the markers.

For example:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.scatter(x, y, s=100, c='red', marker='x')
plt.show()

In this example, the s argument sets the size of the markers to 100, the c argument sets the color of the markers to red, and the marker argument sets the shape of the markers to an “x”.

Note that there are many other customization options available for the scatter() function, including the ability to add labels, change the axis limits, and more. You can find more information and examples in the Matplotlib documentation:
https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.scatter.html