Introduction
Sometimes you want to show two sets of data on the same chart but they have very different sizes. Dual y-axis helps by giving each data set its own scale on the left and right sides.
Sometimes you want to show two sets of data on the same chart but they have very different sizes. Dual y-axis helps by giving each data set its own scale on the left and right sides.
import matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(x, y1, 'color1') ax2.plot(x, y2, 'color2') ax1.set_ylabel('Label for y1') ax2.set_ylabel('Label for y2') plt.show()
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [10, 20, 30, 40] y2 = [100, 200, 300, 400] fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(x, y1, 'g-') ax2.plot(x, y2, 'b--') ax1.set_ylabel('Small scale', color='g') ax2.set_ylabel('Large scale', color='b') plt.show()
import matplotlib.pyplot as plt x = range(5) y1 = [1, 4, 9, 16, 25] y2 = [100, 80, 60, 40, 20] fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(x, y1, 'r-o') ax2.plot(x, y2, 'b-s') ax1.set_ylabel('Squares', color='r') ax2.set_ylabel('Decreasing values', color='b') plt.show()
This program plots temperature as a red line and rainfall as blue bars on the same chart. Each has its own y-axis scale.
import matplotlib.pyplot as plt # Data for plotting months = ['Jan', 'Feb', 'Mar', 'Apr', 'May'] temperature = [30, 32, 35, 33, 31] # degrees Celsius rainfall = [50, 40, 60, 70, 55] # millimeters fig, ax1 = plt.subplots() # Plot temperature on left y-axis ax1.plot(months, temperature, 'r-o') ax1.set_ylabel('Temperature (°C)', color='r') ax1.set_xlabel('Month') # Create second y-axis for rainfall ax2 = ax1.twinx() ax2.bar(months, rainfall, alpha=0.3, color='b') ax2.set_ylabel('Rainfall (mm)', color='b') plt.title('Monthly Temperature and Rainfall') plt.show()
Make sure to use different colors or styles for each axis to avoid confusion.
Dual y-axis can be confusing if overused; keep charts simple and clear.
Dual y-axis lets you show two data sets with different scales on one plot.
Use ax.twinx() to create the second y-axis.
Label each axis clearly and use different colors for clarity.
twinx() creates a second y-axis sharing the x-axis.twiny() creates a second x-axis, not y-axis; subplots() creates figure and axes; plot() draws lines.import matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot([1, 2, 3], [10, 20, 30], 'g-') ax2.plot([1, 2, 3], [100, 200, 300], 'b-') print(len(fig.axes))
plt.subplots() creates one axes (ax1), then ax1.twinx() adds a second y-axis (ax2), so total axes are 2.fig.axes is a list of all axes in the figure; after adding ax2, length is 2.import matplotlib.pyplot as plt fig, ax = plt.subplots() ax2 = plt.twinx() ax.plot([1,2,3], [10,20,30]) ax2.plot([1,2,3], [100,200,300]) plt.show()
twinx() is a method of an axes object, not plt module. So plt.twinx() is invalid.ax.twinx() to create the second y-axis sharing the x-axis.set_ylabel() on each axis with matching labels.Pass the test and this pattern moves unknown