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

Make it ready to leave the notebook.

Export readable charts with units, annotations, and intentional styling.

ADVANCED LAB
BY NAVAN SAMI◷ 11 MIN READPYTHON 3

01 The idea

A chart becomes a deliverable when someone can understand it without your commentary. Add a clear question or title, meaningful units, readable labels, and a restrained style. Export size and resolution depend on where the result will be used. PNG is useful for raster previews; SVG or PDF can preserve vector elements for scalable layouts.

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_export.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
with plt.rc_context({"font.size": 10, "axes.spines.top": False, "axes.spines.right": False}):
    fig, ax = plt.subplots(figsize=(6.5, 3.6), layout="constrained")
    days, total = [1, 2, 3, 4], [2, 5, 9, 12]
    ax.plot(days, total, "o-", color="#ed5027", linewidth=2)
    ax.annotate("Milestone", xy=(3, 9), xytext=(1.6, 10.5), arrowprops={"arrowstyle": "->", "color": "#536643"})
    ax.set(xlabel="Day", ylabel="Completed tasks", title="Orbit reaches the first milestone")
    ax.set_ylim(0, 14)
    fig.savefig("report.svg", bbox_inches="tight")
fig.savefig("matplotlib-export.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved matplotlib-export.png")
↳ EXPECTED OUTPUT
Saved matplotlib-export.png
Annotations, styles & export: chart generated by the Python example
THE OUTPUT, MADE VISIBLE · Generated from this lesson’s example.
Download chart ↗

03 Under the hood

  1. 1

    rc_context temporarily changes style settings, avoiding permanent global changes that affect unrelated figures.

  2. 2

    The annotation points to a specific observation rather than floating without context. Keep it clear of other data and test the final layout at its display size.

  3. 3

    The example saves both SVG and PNG. Real datasets may require provenance, uncertainty, and methodological notes in the surrounding report; an attractive chart is not a substitute for sound analysis.

04 Your turn, builder

A SMALL MISSION

Export a bar chart as PNG and SVG with a title that states the unit.

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.bar(["Build", "Test"], [40, 20], color="#ed5027")
ax.set(title="Planned effort (minutes)", ylabel="Minutes")
fig.savefig("challenge.svg", bbox_inches="tight")
fig.savefig("challenge.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved challenge.png")

05 Check your signal

QUICK SIGNAL CHECK

What does rc_context help you avoid?

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.