Charts with matplotlib · lesson 8 of 11
Parts in this lesson
Aax.set_ylim(0, 50)where the y-axis starts and ends
Bax.set_yscale("log")equal steps for equal ratios
Cax2 = ax.twinx()a second y-axis, to recognise and avoid
Dplt.subplots(2, 1, sharex=True)6.5: two charts, one time axis

Scales and axes

Last updated

Where an axis starts and how it's spaced decides what the reader sees. Start a bar chart's axis at 99 and a 2% rise looks like a boom; draw two measures on two y-axes and you can make them cross wherever you like. In this lesson you'll learn when an axis must start at zero and when it needn't, when a log scale tells the truth better, and why two y-axes on one chart are best avoided.

By the end of this lesson you can

  • Start bar charts at zero, and say why lines don't have to
  • Use a log scale for numbers that grow by multiplying
  • Replace a dual-axis chart with charts that share an x-axis

Wrong way: a 2% boom

A bar shows a value by its length. That only works if the length is measured from zero. Drag the slider to move where the y-axis starts, and watch November's bar.

Truncation trapsales index, November against October
0265278104OctoberNovember
Real change
2%
How much taller November’s bar looks
2%
Exaggerated
1.0 times

At 0, November's bar is 2% taller, which is the truth. At 90 it looks 20% taller; at 99 it's three times October's height. The data didn't change, only where the axis starts. Here it is in matplotlib. Run the cells.

boom.ipynb

ax.set_ylim(99.5, 102.2) cuts off the bottom of both bars, so November looks five times as tall as October (2.5 units against 0.5) and the title can shout "booming". It's a common trick in advertising, and a common accident in reports: matplotlib never starts a bar chart's axis anywhere but zero by itself, but it takes one line to change. Studies of readers find the exaggeration works even on people who notice the axis.

The second cell lets the axis start at zero and gives the honest title, "Sales up 2%". Up 2% is still worth saying. The check: a bar chart's value axis starts at zero, always. If the differences are too small to see from zero, a bar chart is the wrong chart: show the change itself (+2%), or use dots or a line.

Lines and log scales

Lines are different. A line shows change by its slope, not by its length from the axis, so a line chart's y-axis can start where the data is. Run the first cell: a year of rice prices.

scales.ipynb

From zero, the rise from ₱44 to ₱48 a kilo is nearly flat. Zoomed in, it's clear. Both are honest, because the tick labels say where the axis is; choose the one that matches how much the change matters. A ₱4 rise is 9%, which matters to a store.

The second cell is about a different problem: numbers that grow by multiplying. A new online shop's orders go 20, 30, 50, 80, … 1,300 in ten months. On a normal (linear) scale, the first months are squashed flat at the bottom, and the last two months look like an explosion. On a log scale, ax.set_yscale("log"), each step up the axis multiplies by the same amount (10, 100, 1,000), so steady growth by the same percentage draws a straight line. The third cell shows the orders grew by about 1.6 times a month, every month: the straight line on the right says exactly that. Nothing exploded in month 10; it was the same growth all along.

Two more things about the shape of a chart:

  • Aspect ratio changes slopes. The same line looks steep in a tall chart and flat in a wide one. Cleveland and his colleagues found slopes are judged best when the important ones are near 45° ("banking"). In practice, wide charts suit long time series, and you should compare lines in charts of the same shape.
  • Label log axes clearly. Many readers assume a linear scale, so say "log scale" in the axis label or title.

Two y-axes, and what to do instead

A dual-axis chart puts two measures with different units on one chart: one y-axis on the left, one on the right. ax.twinx() makes the second. Run the first two cells: Lipa's daily kilos (left) and Lipa City's daily rain (right).

twin.ipynb

The chart invites you to compare the line with the bars: where they cross, which is "higher". But the two scales are unrelated, and each was chosen by whoever drew the chart. Change ax2.set_ylim(0, 40) to (0, 200) and the rain shrinks to a stubble beneath the line; change it to (0, 10) and the rain towers over it. Any story you like, with the same data. And the title's question is backwards anyway: rain doesn't follow sales.

The third cell is the honest version: two charts, one above the other, sharing the x-axis (sharex=True, 6.5). Each has its own scale and label, and the shared dates line the days up, so you can still look straight down from a rainy day to that day's sales. The first cell already measured what the dual axis only hinted at: on rainier days, Lipa sells less (correlation −0.45). Stage 8 is about how much a number like that can tell you.

The check: if you're about to call twinx, ask whether the reader needs the two measures on one chart. Usually two charts with a shared x-axis, or one measure divided by the other, does the job without a scale you can tune to tell a story.

Check what you learned

A bar chart’s axis starts at 95. The bars are 96 and 98. How many times taller does the second bar look?
Predict: on a log scale, what does growth of 50% a month look like?
You want to show daily sales and daily rain together. What’s best?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis