How do you change the marker size in a Matplotlib pyplot scatter plot? | A1Lab@MZeeshanM to PythonEnglish • 5 days ago

How do you change the marker size in a Matplotlib pyplot scatter plot?

message-square
0

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

  • s in scatter() is area ($\text{pt}^2$), NOT radius or diameter: Unlike plt.plot(..., markersize=10) where markersize (or ms) specifies diameter in points, scatter(..., s=100) specifies area in square points ($\text{pt}^2$). Consequently, doubling a marker’s visual width/diameter requires multiplying s by 4 ($s = \text{diameter}^2$, so markersize=10 in plot() equals s=100 in scatter()).
  • 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 to s. Normalize or scale your size column into a comfortable visual range (e.g., 50 to 800 $\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 passing markerscale to ax.legend(markerscale=0.5) (multiplier applied to marker diameter).
  • Changing the global default scatter marker size (plt.rcParams): To change the default s across every scatter() call in your script without passing s= repeatedly, set plt.rcParams["lines.markersize"] = 10 (remembering that lines.markersize is a diameter in points, so 10 sets default scatter s to $10^2 = 100$).
  • Updating marker sizes dynamically after plotting (animations / interactive plots): ax.scatter() returns a PathCollection artist; update its sizes in place via scatter_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)