typestar

Figure and axes in Python

The object API: one figure, one or more axes, and everything hangs off them.

import matplotlib

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

figure, axes = plt.subplots(figsize=(6, 3), dpi=120)
axes.plot([1, 2, 3], [2, 4, 3])
axes.set_title("first plot")

print(type(figure).__name__, type(axes).__name__)
figure.savefig("first.png")
plt.close(figure)

How it works

  1. subplots returns the figure and its axes together.
  2. Draw on the axes, save from the figure.
  3. The Agg backend renders to a file with no window.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
284
Tokens
94
Three-star pace
100 tpm

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

Type this snippet

Step 1 of 4 in Figures & axes, step 1 of 14 in Plotting with matplotlib.

Next →