Honest charts
Not met yet
A chart makes a claim, so it can lie even when every number on it is right. Bars show a value by their length, so a bar chart's scale starts at 0: ax.set_ylim(bottom=0). A line may fit its scale to the data, which matplotlib does by default, but then the axis must say so, and a change is often clearer drawn as a difference from an average, around a 0 that means something. Panels meant to be compared share one scale (plt.subplots(1, 2, sharey=True)). Missing readings stay missing: NaN leaves a gap in a line, and the gap is the truth. Every axis says what it measures and in what units.
Try it
main.pyimport matplotlib.pyplot as plt
decades = ["1920s", "1960s", "2020s"]
temps = [11.1, 10.9, 12.3]
fig, (bars, change) = plt.subplots(1, 2, figsize=(9, 3))
bars.bar(decades, temps)
bars.set_ylim(bottom=0)
bars.set_ylabel("temperature (degrees C)")
average = sum(temps) / len(temps)
change.bar(decades, [t - average for t in temps])
change.axhline(0, color="grey")
change.set_ylabel("difference from average (degrees C)")
plt.show()
Output
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks matplotlib starts every y axis at zero unless told otherwise. It fits a line's scale to the data; only bars are drawn up from zero.
- Thinks matplotlib fits a bar chart's scale to the data the way it does a line's, when it keeps zero on the scale because a bar's length is its value.
- Thinks starting a bar chart's scale above zero only zooms in. It changes how long the bars look, so the ratios between them lie.
- Thinks the panels from plt.subplots share a scale automatically. Each fits its own unless sharey=True.
- Thinks matplotlib draws a line straight across missing values, or that a gap should be filled in to keep the line tidy. NaN leaves a gap, and the gap is the truth.