0
0
Matplotlibdata~20 mins

Dual y-axis for different scales in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dual Y-Axis Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dual y-axis plot code

What will be the output of this Python code using matplotlib?

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [10, 20, 30, 40, 50]
y2 = [100, 200, 300, 400, 500]
fig, ax1 = plt.subplots()
ax1.plot(x, y1, 'g-')
ax2 = ax1.twinx()
ax2.plot(x, y2, 'b--')
plt.show()
AA single plot with two y-axes: left y-axis green solid line for y1, right y-axis blue dashed line for y2
BTwo separate plots stacked vertically, one with green line and one with blue dashed line
CA plot with one y-axis showing both green and blue lines overlapping
DSyntaxError due to incorrect use of twinx()
Attempts:
2 left
💡 Hint

Remember that twinx() creates a second y-axis sharing the same x-axis.

data_output
intermediate
1:30remaining
Number of ticks on each y-axis

Given this code snippet, how many major ticks will appear on the left and right y-axes respectively?

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [10, 20, 30, 40, 50]
y2 = [100, 200, 300, 400, 500]
fig, ax1 = plt.subplots()
ax1.plot(x, y1, 'r-')
ax2 = ax1.twinx()
ax2.plot(x, y2, 'b-')
left_ticks = len(ax1.get_yticks())
right_ticks = len(ax2.get_yticks())
print(left_ticks, right_ticks)
A5 6
B5 5
C6 5
D6 6
Attempts:
2 left
💡 Hint

Default matplotlib y-axis ticks usually include the min and max plus intermediate ticks.

🔧 Debug
advanced
2:00remaining
Identify the error in dual y-axis code

What error will this code produce?

Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [10, 20, 30]
y2 = [100, 200, 300]
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y1, 'r-')
ax2.plot(x, y2, 'b-')
ax1.set_ylabel('Y1')
ax2.set_ylabel('Y2')
ax1.set_xlabel('X')
ax2.set_xlabel('X')
plt.show()
ANo error; plot shows correctly
BAttributeError: 'AxesSubplot' object has no attribute 'set_xlabel'
CThe second set_xlabel call overwrites the first, no error but confusing labels
DRuntimeWarning: Multiple x-labels set on the same axes
Attempts:
2 left
💡 Hint

Check if setting xlabel on both axes causes an error or just overwrites.

visualization
advanced
1:30remaining
Visual difference between single and dual y-axis plots

Which option best describes the visual difference between these two plots?

Plot 1: Single y-axis plotting y1 and y2 on the same scale.
Plot 2: Dual y-axis plotting y1 on left and y2 on right with different scales.

APlot 1 shows two separate plots; Plot 2 shows one plot with one y-axis
BPlot 1 has two y-axes; Plot 2 has one y-axis
CPlot 1 and Plot 2 look identical
DPlot 1 shows overlapping lines possibly distorted; Plot 2 shows clear lines with separate scales
Attempts:
2 left
💡 Hint

Think about how different scales affect line visibility on one axis.

🧠 Conceptual
expert
1:30remaining
Reason to use dual y-axis in data visualization

Why would a data scientist choose to use a dual y-axis plot?

ATo create two separate plots stacked vertically for better clarity
BTo plot two variables with identical scales for simplicity
CTo compare two variables with very different scales on the same x-axis clearly
DTo avoid labeling axes and keep the plot minimal
Attempts:
2 left
💡 Hint

Think about when one y-axis scale is not enough to show both data sets clearly.