Follow the signal.
Compare ordered series without hiding which line means what.
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.
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")Saved matplotlib-lines.png

03 Under the hood
- 1
Each call adds a separate series to the same Axes. The labels become legend entries.
- 2
Markers and line styles distinguish series even when colour is hard to perceive or the image is printed in greyscale.
- 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
Add a third line representing a constant target of five tasks per day.
Reveal one possible solution +
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
What order does plot connect points in?
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 ↗.