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



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)

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')

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: