bhylu.com

Advertisements

Matplotlib Line

Line styling controls how a series is drawn, but the data relationship should remain the main message. Use a small consistent style vocabulary.

Understand the idea

A dashed line can distinguish a plan from an observed solid line. Width, colour, and labels should work together rather than introduce several unrelated meanings. A legend connects each style to its series. Avoid styling a forecast as an observed fact merely because it produces a cleaner chart.

Where to try it

Run this complete script in a local virtual environment with Matplotlib installed (py -m pip install matplotlib). It uses a non-interactive backend and saves workshop.png in the current folder. The site shows a rendered example below; its text-only online runner does not display plot windows.

A worked example

Python

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4), layout="constrained")
ax.plot([1, 2, 3], [10, 12, 14], label="Plan", linestyle="--")
ax.plot([1, 2, 3], [9, 13, 12], label="Observed", linewidth=2)
ax.set(xlabel="Week", ylabel="Orders", title="Plan and observed orders")
ax.legend()
fig.savefig("workshop.png", dpi=140)
plt.close(fig)
print("Saved workshop.png")

Read the result

Two labelled lines compare planned and observed counts without claiming they are the same kind of evidence.

Two labelled lines compare planned and observed counts without claiming they are the same kind of evidence.
Rendered from this original example. Change and rerun the script locally to make your own chart.

Try one change

Swap line colours while preserving labels and line styles, then check that the chart still communicates the difference.

For package options, see the Matplotlib documentation.