What if you could see two very different data stories clearly on one graph without confusion?
Why Dual y-axis for different scales in Matplotlib? - Purpose & Use Cases
Imagine you have two sets of data to compare: one shows daily temperatures in degrees, and the other shows rainfall in millimeters. You want to see both on the same chart to understand how weather changes together.
Trying to plot both on one y-axis makes one set look tiny or huge because their scales are very different. Manually adjusting or guessing scales is slow and confusing, and you might misread the data.
Using a dual y-axis lets you plot both data sets clearly on the same chart, each with its own scale. This way, you can easily compare trends without mixing up values.
plt.plot(temp) plt.plot(rainfall) plt.show()
fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(temp, 'r-') ax2.plot(rainfall, 'b-') plt.show()
You can visualize and compare two different types of data clearly on one graph, making insights easier and faster to find.
A weather analyst compares temperature and rainfall trends over a month to predict floods or droughts by using dual y-axes in one chart.
Manual plotting mixes scales and confuses data.
Dual y-axis separates scales but shares the same x-axis.
This method makes comparing different data types simple and clear.
Practice
Solution
Step 1: Understand the purpose of dual y-axis
A dual y-axis allows plotting two datasets with different value ranges on the same graph for easy comparison.Step 2: Identify the correct use case
Comparing datasets with different scales requires two y-axes; other options do not describe this purpose.Final Answer:
To compare two datasets with different scales on the same plot -> Option AQuick Check:
Dual y-axis = compare different scales [OK]
- Thinking dual y-axis creates two separate plots
- Confusing dual y-axis with adding legends
- Assuming it changes line colors only
Solution
Step 1: Recall matplotlib methods for axes
The methodtwinx()creates a second y-axis sharing the x-axis.Step 2: Differentiate from similar methods
twiny()creates a second x-axis, not y-axis;subplots()creates figure and axes;plot()draws lines.Final Answer:
ax.twinx() -> Option CQuick Check:
Second y-axis = twinx() [OK]
- Using plt.subplots() to create second y-axis
- Confusing twinx() with twiny()
- Trying to use plt.plot() to add axes
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))
Solution
Step 1: Understand axes creation
plt.subplots()creates one axes (ax1), thenax1.twinx()adds a second y-axis (ax2), so total axes are 2.Step 2: Check figure axes count
fig.axesis a list of all axes in the figure; after addingax2, length is 2.Final Answer:
2 -> Option AQuick Check:
Two axes after twinx() = 2 [OK]
- Assuming twinx() replaces the original axes
- Thinking twinx() adds multiple axes
- Confusing axes count with number of plots
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()
Solution
Step 1: Check method usage for second y-axis
twinx()is a method of an axes object, not plt module. Soplt.twinx()is invalid.Step 2: Correct method call
Useax.twinx()to create the second y-axis sharing the x-axis.Final Answer:
plt.twinx() should be ax.twinx() -> Option DQuick Check:
twinx() is an axes method, not plt function [OK]
- Calling twinx() on plt instead of axes
- Confusing figure and axes creation methods
- Missing plt.show() (though present here)
Solution
Step 1: Assign correct y-axis labels
Temperature and rainfall are y-axis data, so useset_ylabel()on each axis with matching labels.Step 2: Use colors to distinguish axes
Setting label colors helps visually separate the two y-axes for clarity.Final Answer:
ax1.set_ylabel('Temperature (°C)', color='red')\nax2.set_ylabel('Rainfall (mm)', color='blue') -> Option BQuick Check:
Correct y-labels and colors = ax1.set_ylabel('Temperature (°C)', color='red')\nax2.set_ylabel('Rainfall (mm)', color='blue') [OK]
- Setting x-axis labels instead of y-axis
- Swapping labels between axes
- Using titles instead of axis labels
Ready to prove it?
Pass the test and this pattern moves unknown
