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

Follow the signal.

Compare ordered series without hiding which line means what.

ADVANCED LAB
BY NAVAN SAMI◷ 11 MIN READPYTHON 3

01 The idea

A line chart is useful when the order of x values carries meaning, such as time or a controlled sequence. Each line joins observations in the order supplied. Multiple series need labels and a legend, and missing observations need an intentional policy: joining across a gap can imply evidence you do not have.

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_lines.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
days = [1, 2, 3, 4]
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
ax.plot(days, [3, 5, 4, 7], "o-", label="Completed", color="#ed5027")
ax.plot(days, [4, 4, 6, 6], "s--", label="Planned", color="#48643a")
ax.set(xlabel="Day", ylabel="Tasks", title="Plan and reality")
ax.legend(frameon=False)
ax.set_xticks(days)
fig.savefig("matplotlib-lines.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved matplotlib-lines.png")
↳ EXPECTED OUTPUT
Saved matplotlib-lines.png
Line charts, markers & legends: chart generated by the Python example
THE OUTPUT, MADE VISIBLE · Generated from this lesson’s example.
Download chart ↗

03 Under the hood

  1. 1

    Each call adds a separate series to the same Axes. The labels become legend entries.

  2. 2

    Markers and line styles distinguish series even when colour is hard to perceive or the image is printed in greyscale.

  3. 3

    Sort chronological data before plotting, and use actual date values when spacing between dates matters. Do not sort x independently of y; keep each observation paired.

04 Your turn, builder

A SMALL MISSION

Add a third line representing a constant target of five tasks per day.

Reveal one possible solution +
one_possible_solution.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
days = [1, 2, 3, 4]
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
ax.plot(days, [3, 5, 4, 7], "o-", label="Completed", color="#ed5027")
ax.axhline(5, linestyle="--", color="#48643a", label="Target")
ax.set(xlabel="Day", ylabel="Tasks", title="Progress versus target")
ax.legend()
fig.savefig("challenge.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved challenge.png")

05 Check your signal

QUICK SIGNAL CHECK

What order does plot connect points in?

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.