Create a scatter plot in Matplotlib

Python

Matplotlib

Scatter

Create

Luc B.

Python

Matplotlib

Scatter Plot

Scatter plots are powerful tools for observing trends in unordered data. While basic scatter plots should be created with plt.plot() to improve performance, Matplotlib implements plt.scatter() for users who want to vary the marker size and color to visually encode additional dimensions of data.

Code Example

Use the plt.scatter() function to create a scatter plot in Matplotlib.

import matplotlib.pyplot as plt

x_values = [20, 19, 16, 12, 19, 18, 22, 14]
y_values = [1, 0.91, 0.77, 0.67, 0.85, 0.78, 1.05, 0.63]

# Create scatter plot here
plt.scatter(x_values, y_values)

plt.xlabel('Number of Dance Moves')
plt.ylabel('Cool Factor')
plt.show()

png

More Examples

Use plt.plot()

For scatter plots where the marker size and color are constant, use plt.plot() instead of plt.scatter(). Simply pass an o character as the third parameter so the markers are points instead of lines.

# Create scatter plot here
plt.plot(x_values, y_values, 'o')

plt.xlabel('Number of Dance Moves')
plt.ylabel('Cool Factor')
plt.show()

png

plt.plot() versus plt.scatter()

As noted in Jake Vanderplas's Python Data Science Handbook, plt.plot() does not allow users to individually style markers—each marker is a clone of the previous. As a result, plt.plot() is more performant than plt.scatter(), and it should be preferred over plt.scatter() if the capacities of plt.scatter() aren't needed.