Pass the s parameter (representing marker area in points squared, $\text{pt}^2$, not diameter) to plt.scatter(x, y, s=...) or ax.scatter(x, y, s=...)—using a single numeric scalar to resize all markers uniformly, or passing a 1D array/Series of the same length as x and y to scale each point dynamically (bubble chart).
Basic Example
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 18, 22]
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 3.8), layout="constrained")
# 1. UNIFORM SIZE: Pass a scalar value to 's' (default is ~36, i.e., 6^2)
ax1.scatter(x, y, s=150, color="steelblue", alpha=0.8)
ax1.set_title("Uniform Marker Size (s=150)")
# 2. VARIABLE SIZE (Bubble Plot): Pass a list/array matching the length of x and y
sizes = [40, 120, 250, 450, 700]
ax2.scatter(x, y, s=sizes, color="coral", alpha=0.75, edgecolors="black")
ax2.set_title("Variable Marker Sizes (s=sizes)")
plt.show()
Key Details & Edge Cases
sinscatter()is area ($\text{pt}^2$), NOT radius or diameter: Unlikeplt.plot(..., markersize=10)wheremarkersize(orms) specifies diameter in points,scatter(..., s=100)specifies area in square points ($\text{pt}^2$). Consequently, doubling a marker’s visual width/diameter requires multiplyingsby 4 ($s = \text{diameter}^2$, somarkersize=10inplot()equalss=100inscatter()).- Scaling data values cleanly for bubble plots: Raw data values (like populations in millions or tiny probabilities like
0.002) will either cover the entire canvas or vanish if passed directly tos. Normalize or scale your size column into a comfortable visual range (e.g.,50to800$\text{pt}^2$):import numpy as np raw_values = np.array([5, 20, 50, 100]) scaled_s = (raw_values / raw_values.max()) * 600 # Scales max bubble to 600 pt^2 ax.scatter(x[:4], y[:4], s=scaled_s) - Resizing markers inside the legend (
markerscale): When a scatter plot uses huge markers (s=500) or tiny markers (s=5), the legend icon mirrors that exact size by default. Scale legend markers independently without affecting the plot by passingmarkerscaletoax.legend(markerscale=0.5)(multiplier applied to marker diameter). - Changing the global default scatter marker size (
plt.rcParams): To change the defaultsacross everyscatter()call in your script without passings=repeatedly, setplt.rcParams["lines.markersize"] = 10(remembering thatlines.markersizeis a diameter in points, so10sets default scattersto $10^2 = 100$). - Updating marker sizes dynamically after plotting (animations / interactive plots):
ax.scatter()returns aPathCollectionartist; update its sizes in place viascatter_artist.set_sizes([100, 200, ...]).
Comparison of plt.scatter() vs. plt.plot() Marker Sizing
| Feature | ax.scatter(x, y, s=...) |
ax.plot(x, y, "o", markersize=...) |
|---|---|---|
| Parameter Name | s |
markersize (or alias ms) |
| Unit of Measurement | Area in square points ($\text{pt}^2$) | Diameter / width in points ($\text{pt}$) |
| Default Value | rcParams["lines.markersize"] ** 2 (default 36.0) |
rcParams["lines.markersize"] (default 6.0) |
| Equivalent Visual Size | s=100 |
markersize=10 ($10^2 = 100$) |
| Per-Point Variable Sizing? | Yes (s=[20, 80, 300, ...]) |
No (single uniform size for the entire series) |
You must log in or register to comment.
