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
subplotsreturns the figure and its axes together.- Draw on the axes, save from the figure.
- The Agg backend renders to a file with no window.
Keywords and builtins used here
asprinttype
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.
Step 1 of 4 in Figures & axes, step 1 of 14 in Plotting with matplotlib.