When creating scatter plots using Matplotlib, one of the key parameters to consider is the marker size, which is specified by the s
argument in the plt.scatter()
function. The marker size is defined in points^2, where a point is a unit of measurement equal to 1/72 inches. In this tutorial, we will explore how to understand and work with the marker size parameter in Matplotlib scatter plots.
To begin with, it’s essential to recognize that the s
argument does not directly specify the diameter or radius of the marker, but rather its area. This means that if you want to double the width (or height) of a marker, you need to increase the s
value by a factor of 4, since the area is proportional to the square of the width.
Here’s an example code snippet that demonstrates this concept:
import matplotlib.pyplot as plt
x = [0, 2, 4, 6, 8, 10]
y = [0] * len(x)
s = [20 * 4 ** n for n in range(len(x))]
plt.scatter(x, y, s=s)
plt.show()
This code will produce a scatter plot with markers of increasing size, where each marker is twice as wide (and therefore four times as large in area) as the previous one.
Another important aspect to consider when working with marker sizes is the relationship between points and pixels. Since Matplotlib uses points as its default unit of measurement, it’s sometimes necessary to convert between points and pixels, especially when creating plots for display on screens or other digital devices.
The conversion between points and pixels depends on the figure’s DPI (dots per inch), which can be set using the fig.dpi
attribute. For example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(1.5, 2), dpi=100)
# Create a scatter plot with markers of size 10 points^2
ax.scatter([0], [1], s=10 ** 2, marker="s", linewidth=0)
# Save the figure to a file
fig.savefig("plot.png", bbox_inches="tight")
In this example, the figure’s DPI is set to 100, which means that one point is equivalent to 100/72 pixels.
To create markers of a specific size in pixels, you can use the following formula:
s = (size_in_pixels * fig.dpi / 72) ** 2
Where size_in_pixels
is the desired size of the marker in pixels.
In conclusion, understanding how to work with marker sizes in Matplotlib scatter plots requires a basic grasp of the relationship between points and pixels, as well as the concept of area as it relates to marker size. By using the techniques outlined in this tutorial, you should be able to create high-quality scatter plots with markers of varying sizes that accurately convey your data.
Here’s an example code snippet that demonstrates how to create a scatter plot with markers of different sizes:
import matplotlib.pyplot as plt
x = [0, 1, 2]
y = [0, 1, 2]
s = [10 ** 2, 50 ** 2, 100 ** 2]
plt.scatter(x, y, s=s)
plt.show()
This code will produce a scatter plot with three markers of increasing size, each representing a different data point.