You are plotting two datasets with different scales on y-axis. How can you plot them on the same graph, ensuring clarity by using dual y-axis?
You are plotting two datasets with different scales on y-axis. How can you plot them on the same graph, ensuring clarity by using dual y-axis?
Solution
ax1.twinx() creates a second axes that shares the same x-axis but has its own independent y-axis, drawn on the right side. Plot one dataset on ax1 and the other on ax2, and each gets its own y-scale on the same graph.
Options 2 and 3: plt.subplots(2,1) and plt.subplots(1,2) create separate subplots (stacked or side by side), not one graph. The syntax fig(ax1,ax2) = ... is also invalid.
Option 4: twiny() shares the y-axis and creates a second x-axis, which is the opposite of what is needed.
Remember: twinx = twin x-axis shared, new y-axis. twiny = twin y-axis shared, new x-axis.
Related questions:
2025: 16 May Shift 1
2025: 21 May Shift 1
2025: 30 May Shift 2
2026: 11 May Shift 2
2025: 13 May Shift 2