Individually change the scatter plot marker sizes in Matplotlib

Python

Matplotlib

Scatter

Change marker sizes

Luc B.

Python

Matplotlib

Scatter Plot

An easy way to create more expressive scatter plots is to style the markers so they visually encode additional data. By changing the color, size, and style of the markers, we can communicate more information and trends.

Code Example

Use the s parameter to plt.scatter() to change the marker size. s can be a scalar to uniformly change the marker size, or it can be an array to modify the marker size individually.

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]

# Change the marker size here
marker_size = [40, 80, 120, 120, 150, 170, 40, 40]
plt.scatter(x_values, y_values, s=marker_size)

plt.show()

png

More Examples

Uniformly Change Marker Size

Pass a scalar to the s parameter to uniformly change the marker size.

# Change the marker size here
plt.scatter([1, 2, 3], [2, 3, 4], s=200)
plt.scatter([1, 2, 3], [1, 2, 3], s=30)

plt.legend(['Big Markers', 'Small Markers'])
plt.show()

png