Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11To put two independent data series on one Matplotlib plot with a shared x-axis, create a second Axes with ax1.twinx() and plot the second series on it. If the axes show the same quantity in different units, such as Celsius and Fahrenheit, use secondary_yaxis() with a conversion and its inverse instead.
Choose the right kind of second y-axis
| Use case | API | How the second scale behaves |
|---|---|---|
| Two independent series that share an x-axis | Axes.twinx() |
Creates another Axes with an independent y-axis, normally on the right. Each Axes owns the series plotted on it and can have its own y limits, tick locator, and formatter. Matplotlib Axes.twinx API |
| One quantity expressed on two related scales, such as Celsius and Fahrenheit | Axes.secondary_yaxis() |
Shows a converted scale derived from the parent axis. Supply forward and inverse conversion functions; the parent Axes controls the view limits. Matplotlib secondary_yaxis API |
In short: use twinx() when each series has its own values and scale; use secondary_yaxis() when the second scale is a conversion of the first. A secondary axis is not a place to plot an unrelated second dataset.
Plot two independent series with twinx()
This example puts y1 on the left axis and y2 on the right. Replace the sample arrays with your data; x must contain the corresponding shared x-values.
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1.plot(x, y1, color="tab:red")
ax1.set_xlabel("X")
ax1.set_ylabel("Series 1", color="tab:red")
ax1.tick_params(axis="y", labelcolor="tab:red")
ax2 = ax1.twinx()
ax2.plot(x, y2, color="tab:blue")
ax2.set_ylabel("Series 2", color="tab:blue")
ax2.tick_params(axis="y", labelcolor="tab:blue")
fig.tight_layout()
plt.show()
What the code does
plt.subplots()creates the figure and the first Axes,ax1.ax1.twinx()creates a second Axes that shares the x-axis and has its own y-axis, positioned on the right by default. Ploty2onax2, not onax1. Matplotlib Axes.twinx API- The matching line, label, and tick colors help show which scale belongs to which series.
fig.tight_layout()can help keep the right-side label within the figure. Matplotlib two-scales example
Set each scale independently
Because the two Axes have independent y-axes, set a limit, tick locator, or formatter on the Axes that owns the corresponding series. For example, ax1.set_ylim(...) affects the left scale, while ax2.set_ylim(...) affects the right. Choose limits that preserve the meaning of each dataset rather than adjusting them solely to make the lines overlap.
#1 Best Overall
Use secondary_yaxis() for convertible units
When both sides represent the same underlying quantity, define a forward conversion from the primary scale and an inverse conversion back. For Celsius and Fahrenheit, the conversion is F = C * 9 / 5 + 32, and its inverse is C = (F - 32) * 5 / 9.
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
fig, ax = plt.subplots()
ax.plot(x, temperature_c)
ax.set_ylabel("Temperature (°C)")
secax = ax.secondary_yaxis(
"right",
functions=(celsius_to_fahrenheit, fahrenheit_to_celsius),
)
secax.set_ylabel("Temperature (°F)")
fig.tight_layout()
plt.show()
The conversion functions need to work with NumPy arrays. The secondary axis derives its range from the parent Axes through those functions; setting limits directly on the secondary axis does not control the displayed view. Matplotlib labels the secondary_yaxis() API experimental, so check the documentation and compatibility for the Matplotlib release you use. The stable documentation surfaced here is labeled Matplotlib 3.11.2; that does not establish which version is installed in your environment. Matplotlib secondary_yaxis API
Quick Recap
Best Value
Rank #4
Rank #2
Common issues with twinned axes
- The right-axis label is clipped: call
fig.tight_layout()and check the saved or displayed figure. Layout behavior can depend on how and where the figure is rendered. - It is unclear which scale belongs to which line: use distinct line colors and repeat those colors for the matching y-axis label and tick labels.
- A line appears misleadingly aligned with the other: remember that each y-axis can have independent limits. The apparent crossing or overlap depends on those limits, so it does not by itself establish a relationship between the series.
- Pick events miss artists: with twinned Axes, Matplotlib calls pick events only for artists in the top-most Axes. This matters when building interactive plots. Matplotlib Axes.twinx API
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




