typestar

Scatter plots in Python

Point size and color are two more dimensions you can spend.

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng(0)
x = rng.normal(size=200)
y = x * 1.5 + rng.normal(scale=0.5, size=200)
weight = np.abs(y)

figure, axes = plt.subplots()
points = axes.scatter(x, y, c=weight, s=18, alpha=0.7, cmap="viridis")
figure.colorbar(points, ax=axes, label="distance from zero")
axes.set_xlabel("x")
axes.set_ylabel("y")
figure.savefig("scatter.png")

How it works

  1. s sizes the markers, c colors them by value.
  2. alpha reveals overlap in a dense cloud.
  3. A colorbar explains what the color means.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
448
Tokens
143
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 82 seconds.

Type this snippet

Step 2 of 5 in Plot types, step 6 of 14 in Plotting with matplotlib.

← Previous Next →