Paul's Python CoursePPC

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.py

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.

Where it comes up