Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Make the data visible
EXPERIMENT 49 / 101

A blank canvas with coordinates.

Create an explicit Figure and Axes, then save your first real chart.

ADVANCED LAB
BY NAVAN SAMI◷ 10 MIN READPYTHON 3

01 The idea

A Figure is the complete canvas; an Axes is a plotting region inside it. Using fig, ax = plt.subplots() makes that ownership explicit and scales well when a report contains several panels. This course saves charts with the noninteractive Agg backend so the scripts work in terminals and headless environments. Open the resulting PNG to inspect it; interactive notebooks can also display figures directly.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

matplotlib_first_figure.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
ax.plot([1, 2, 3], [2, 5, 4], marker="o", color="#ed5027")
ax.set(title="A first signal", xlabel="Experiment", ylabel="Score")
ax.grid(axis="y", alpha=0.2)
fig.savefig("matplotlib-first-figure.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved matplotlib-first-figure.png")
↳ EXPECTED OUTPUT
Saved matplotlib-first-figure.png
Matplotlib: Figure & Axes: chart generated by the Python example
THE OUTPUT, MADE VISIBLE · Generated from this lesson’s example.
Download chart ↗

03 Under the hood

  1. 1

    Install the workbench requirements first. Agg is selected before importing pyplot; backend selection controls rendering rather than the meaning of the data.

  2. 2

    plot receives corresponding x and y values. The marker makes the three observations visible, while the line suggests their sequence.

  3. 3

    Save through fig.savefig, then close the figure to release resources in a batch job. A chart should have units, labels, and enough context for someone who never saw your code.

04 Your turn, builder

A SMALL MISSION

Create a four-point chart of completed tasks across four days and label both axes.

Reveal one possible solution +
one_possible_solution.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
ax.plot([1, 2, 3, 4], [2, 4, 3, 6], marker="o", color="#ed5027")
ax.set(xlabel="Day", ylabel="Completed tasks", title="Daily progress")
fig.savefig("challenge.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved challenge.png")

05 Check your signal

QUICK SIGNAL CHECK

What is an Axes?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.