typestar

Annotations in Python

Pointing at the one data point the reader should notice.

import matplotlib

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

days = [1, 2, 3, 4, 5]
tpm = [88, 91, 104, 97, 99]
best = max(range(len(tpm)), key=lambda i: tpm[i])

figure, axes = plt.subplots()
axes.plot(days, tpm, marker="o")
axes.annotate(f"best {tpm[best]}",
              xy=(days[best], tpm[best]),
              xytext=(days[best] + 0.4, tpm[best] + 4),
              arrowprops={"arrowstyle": "->", "color": "#e06c75"})
axes.text(1.1, 86, "warm up", fontsize=8, color="#646a7a")
axes.set_ylim(84, 112)
figure.savefig("annotated.png")

How it works

  1. annotate takes the text, the point, and where to put the label.
  2. arrowprops draws the connector.
  3. text is the version without an arrow.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
510
Tokens
192
Three-star pace
110 tpm

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

Type this snippet

Step 1 of 4 in Reading the plot, step 10 of 14 in Plotting with matplotlib.

← Previous Next →

Annotations in other languages