A dual y-axis plot shows two different y-axes on the same graph, each with its own scale. This helps compare two datasets with different units or ranges on one plot.
Click to reveal answer
beginner
Which matplotlib function creates a second y-axis sharing the same x-axis?
The twinx() function creates a second y-axis on the right side of the plot that shares the same x-axis as the original plot.
Click to reveal answer
beginner
Why use different colors for lines and y-axis labels in a dual y-axis plot?
Using different colors helps clearly link each line to its corresponding y-axis, making the plot easier to read and understand.
Click to reveal answer
beginner
How do you label the second y-axis in matplotlib?
After creating the second y-axis with twinx(), use ax2.set_ylabel('label') to add a label to the right y-axis.
Click to reveal answer
beginner
What is a common use case for dual y-axis plots?
They are useful when comparing two related datasets with different units or scales, like temperature and rainfall over time.
Click to reveal answer
Which matplotlib method creates a second y-axis sharing the same x-axis?
Aplot()
Btwiny()
Ctwinx()
Dsubplot()
✗ Incorrect
The twinx() method creates a second y-axis on the right side sharing the x-axis.
Why use a dual y-axis plot?
ATo compare two datasets with different y-axis scales
BTo plot two datasets with the same scale
CTo create a 3D plot
DTo plot histograms
✗ Incorrect
Dual y-axis plots help compare datasets with different units or scales on the same x-axis.
How do you label the second y-axis in matplotlib?
Aax.set_xlabel()
Bax.set_ylabel()
Cplt.ylabel()
Dax2.set_ylabel()
✗ Incorrect
After creating the second y-axis with twinx(), use ax2.set_ylabel() to label it.
What is a good practice to distinguish lines in a dual y-axis plot?
AUse different colors for lines and y-axis labels
BUse dashed lines only
CUse the same color for both lines
DUse no labels
✗ Incorrect
Different colors help link each line to its y-axis, improving readability.
Which side of the plot does the second y-axis appear by default?
ALeft
BRight
CTop
DBottom
✗ Incorrect
The second y-axis created by twinx() appears on the right side.
Explain how to create a dual y-axis plot in matplotlib and why it is useful.
Think about plotting two lines with different units on one graph.
You got /4 concepts.
Describe best practices for making dual y-axis plots easy to read.
Consider how to help the viewer quickly understand which line belongs to which axis.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a dual y-axis in a matplotlib plot?
easy
A. To compare two datasets with different scales on the same plot
B. To create two separate plots side by side
C. To add a legend to the plot
D. To change the color of the plot lines
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 A
Quick Check:
Dual y-axis = compare different scales [OK]
Hint: Dual y-axis means two y-scales on one graph [OK]
Common Mistakes:
Thinking dual y-axis creates two separate plots
Confusing dual y-axis with adding legends
Assuming it changes line colors only
2. Which matplotlib function is used to create a second y-axis sharing the same x-axis?
easy
A. plt.subplots()
B. ax.twiny()
C. ax.twinx()
D. plt.plot()
Solution
Step 1: Recall matplotlib methods for axes
The method twinx() 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 C
Quick Check:
Second y-axis = twinx() [OK]
Hint: Use ax.twinx() for second y-axis, twiny() is for x-axis [OK]
twinx() is a method of an axes object, not plt module. So plt.twinx() is invalid.
Step 2: Correct method call
Use ax.twinx() to create the second y-axis sharing the x-axis.
Final Answer:
plt.twinx() should be ax.twinx() -> Option D
Quick Check:
twinx() is an axes method, not plt function [OK]
Hint: twinx() must be called on axes object, not plt [OK]
Common Mistakes:
Calling twinx() on plt instead of axes
Confusing figure and axes creation methods
Missing plt.show() (though present here)
5. You want to plot temperature (°C) and rainfall (mm) over months on the same plot with dual y-axes. Which of these code snippets correctly sets different y-axis labels and colors for clarity?
hard
A. ax1.set_xlabel('Temperature (°C)', color='red')\nax2.set_xlabel('Rainfall (mm)', color='blue')
B. ax1.set_ylabel('Temperature (°C)', color='red')\nax2.set_ylabel('Rainfall (mm)', color='blue')
C. ax1.set_ylabel('Rainfall (mm)', color='blue')\nax2.set_ylabel('Temperature (°C)', color='red')
D. ax1.set_title('Temperature and Rainfall')\nax2.set_title('Temperature and Rainfall')
Solution
Step 1: Assign correct y-axis labels
Temperature and rainfall are y-axis data, so use set_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 B