Skip to main contentSkip to solution

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

✅ Correct Option: 1

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question