TExcelTAD
Home › Matplotlib › Matplotlib Scatter

Matplotlib Scatter

A scatter plot draws one marker per observation, with no connecting lines. It is the standard way to look at the relationship between two numeric variables: does one tend to rise when the other rises?

A basic scatter plot

plt.scatter(x, y) needs two sequences of the same length:

python · try it yourself
import matplotlib.pyplot as plt

study_hours = [1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7, 8, 9]
exam_score = [52, 55, 61, 58, 67, 70, 66, 75, 79, 77, 85, 90]

plt.scatter(study_hours, exam_score)
plt.xlabel("Hours studied")
plt.ylabel("Exam score")
plt.title("Study time vs. score")
plt.show()

print("Students:", len(study_hours))
print("Score range:", min(exam_score), "-", max(exam_score))
Students: 12
Score range: 52 - 90

The points clearly drift upward to the right: a positive correlation.

Comparing two groups

Call scatter() once per group. Each call gets the next colour of the cycle, and a label lets the legend name the groups:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(1)
height_a = rng.normal(170, 7, 40)
weight_a = 0.9 * height_a - 85 + rng.normal(0, 5, 40)
height_b = rng.normal(160, 6, 40)
weight_b = 0.8 * height_b - 75 + rng.normal(0, 4, 40)

plt.scatter(height_a, weight_a, label="Group A")
plt.scatter(height_b, weight_b, label="Group B", marker="^")
plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.legend()
plt.show()

print(f"Group A mean height: {height_a.mean():.1f} cm")
print(f"Group B mean height: {height_b.mean():.1f} cm")
Group A mean height: 170.0 cm
Group B mean height: 159.9 cm

Colour, size and transparency per point

Unlike plot(), scatter() can give every point its own colour and size:

Argument Meaning
c One colour, a list of colours, or a list of numbers mapped through a colormap
s Marker area in points² (a number or one value per point)
cmap Colormap used when c holds numbers, e.g. "viridis"
alpha Transparency 0–1, useful when points overlap
edgecolors Outline colour of the markers
python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(3)
x = rng.uniform(0, 10, 60)
y = rng.uniform(0, 10, 60)
value = x + y                   # mapped to colour
size = rng.uniform(20, 400, 60) # mapped to marker area

plt.scatter(x, y, c=value, s=size, cmap="viridis", alpha=0.7, edgecolors="black")
plt.colorbar(label="x + y")
plt.title("Colour and size per point")
plt.show()

print("Points:", len(x))
print(f"Colour values from {value.min():.2f} to {value.max():.2f}")
Points: 60
Colour values from 1.28 to 18.57

plt.colorbar() adds a scale that translates colours back into numbers. Without it a colour-coded scatter is impossible to read.

Lots of points: use transparency

When thousands of points overlap, a solid blob hides how dense each region is. Small markers with a low alpha reveal the structure:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(11)
x = rng.normal(0, 1, 3000)
y = 0.6 * x + rng.normal(0, 0.8, 3000)

plt.scatter(x, y, s=8, alpha=0.2, color="C2")
plt.title("3000 points with alpha = 0.2")
plt.axis("equal")
plt.show()

r = np.corrcoef(x, y)[0, 1]
print(f"Correlation coefficient: {r:.3f}")
Correlation coefficient: 0.604

np.corrcoef measures the linear relationship: 1 is perfectly positive, 0 none, −1 perfectly negative. plt.axis("equal") makes one unit equally long on both axes.

Note: s is an area. Doubling s makes a marker only about 1.4 times wider. To make the width proportional to a value, pass s=value ** 2.

Test yourself

What is the main difference between plt.scatter() and plt.plot(x, y, "o")?

  1. scatter can't draw circles
  2. scatter can vary colour and size for each point
  3. plot is only for lines
  4. There is no difference

What does the s argument of scatter control?

  1. Marker shape
  2. Marker area in points squared
  3. Line style
  4. The seed

When c contains numbers, what turns them into colours?

  1. The legend
  2. The colormap (cmap)
  3. The alpha value
  4. The grid