Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 57 · Subplots and Layouts

Shared, Twin and Secondary Axes

Use sharex and sharey, twinx for two y scales, and secondary_yaxis with a conversion function in Matplotlib, and learn when dual axes mislead readers.

Sometimes two quantities belong in the same chart: temperature and rainfall over the months, price in dollars and euros, a signal in volts and its power in decibels. Matplotlib offers three different tools, and they solve different problems. Shared axes keep separate panels on the same scale. Twin axes draw two different quantities against one x axis. Secondary axes show the same quantity in a second unit.

Knowing which one you need saves a lot of confusion, because two y scales in one panel are also the easiest way to make a chart that lies. This lesson shows each tool and ends with rules for staying honest. It builds on Multiple Subplots and Limits, Scales and Aspect.

Pick the right tool

Shared axes Separate panels, same scale.
Twin axes Two quantities, one panel.
Secondary axes One quantity, two units.

Only twin axes can mislead. Use them with care.

1 / 2

Shared axes

When you create panels with plt.subplots(..., sharex=True), their x limits stay linked. You can also share an existing pair with ax2.sharex(ax1) (or the sharex=ax1 argument of add_subplot). Linked panels zoom together, and Matplotlib hides duplicate tick labels on inner panels.

Python Stacked panels with a shared x axis

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Setting set_xlim on one panel changed all three, because their x axes are shared. Stacked panels with a shared x axis are often clearer than one chart with three y scales: each quantity gets its own unit, and no scale is distorted.

twinx: two y scales in one panel

ax.twinx() creates a second Axes on top of the first. The two share the x axis, but the new one has its own y axis on the right. You plot the second series on the twin and label each side separately. Color the labels and ticks to match their lines so readers know which scale belongs to which curve.

Python A climograph with two y axes

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is the classic use: a bar for an amount and a line for a different measure over the same time. There is also twiny() for two x axes with a shared y.

Why dual axes are dangerous

The scales of two y axes are independent, and you choose them. By stretching or shifting one axis you can make any two series appear to track each other or to diverge. The same data can look like perfect agreement or none at all, depending only on your limits.

Python Same data, two stories

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two panels plot identical numbers, and only the y limits differ. Both panels are technically correct, which is exactly the problem.

Do not use a twin axis to suggest that two things are correlated. If you need to compare two series with different units, prefer separate stacked panels with a shared x axis, or plot both as an index (each divided by its first value) on one axis.

If you must use twin axes, do these things: color-code the axes, start bars at zero, keep the number of series small (one per side), and say in the title what is on each side.

secondary_yaxis: the same quantity in another unit

A secondary axis does not draw data. It adds a second scale to the same data, for example Celsius on the left and Fahrenheit on the right. You give it a pair of functions: one converting the main unit to the secondary unit, and one converting back. Since both views describe one curve, nothing can mislead.

Python Celsius and Fahrenheit together

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

secondary_xaxis works the same way for x. The two functions must be inverses of each other, and they must work on NumPy arrays (write them with arithmetic, not if statements). Typical pairs are meters and feet, wavelength and frequency (use 1 / x carefully near zero), years and ages, or a linear value and its decibel value on a log axis.

Dual-unit checklist

  • Is it the same quantity in two units? Use secondary_yaxis.
  • Are they different quantities over the same x? Prefer two stacked panels with sharex=True.
  • Do you still want one panel? Use twinx, color-code both sides and avoid implying a relationship.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which call keeps the x limits of stacked panels linked?

    sharex=True links the x axes, so changing the limits of one panel changes them all.
  2. 2. What does ax.twinx() create?

    twinx makes a new transparent Axes over the first with the same x scale and an independent y scale. twiny does the same for x.
  3. 3. Why can twin y axes mislead?

    Nothing ties the two scales together, so by changing the limits you control how the curves line up.
  4. 4. What must you supply to secondary_yaxis for a unit conversion?

    functions=(forward, inverse) lets Matplotlib place ticks correctly in both directions, including on non-linear scales.

Summary

  • Shared axes (sharex, sharey) keep separate panels on one scale and remove duplicate tick labels.
  • ax.twinx() draws a second quantity against the same x with an independent y scale; twiny() does the opposite.
  • Dual y axes can be tuned to show any story, so use them sparingly and color-code each side.
  • secondary_xaxis / secondary_yaxis add a second scale for the same data using a function and its inverse.
  • Stacked panels with a shared x axis are usually the clearest way to compare different quantities.

Next, point the reader's eye with Annotations and Arrows.