Challenge - 5 Problems
Secondary Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of plot with secondary y-axis
What will be the output of this code snippet that uses a secondary y-axis in matplotlib?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [10, 20, 25, 30] y2 = [100, 200, 250, 300] fig, ax1 = plt.subplots() ax1.plot(x, y1, 'g-') ax2 = ax1.twinx() ax2.plot(x, y2, 'b--') plt.show()
Attempts:
2 left
💡 Hint
Remember that twinx() creates a second y-axis sharing the same x-axis.
✗ Incorrect
The twinx() method creates a secondary y-axis on the right side. The first plot uses the left y-axis (green line), and the second plot uses the right y-axis (blue dashed line).
❓ data_output
intermediate1:30remaining
Number of ticks on secondary y-axis
Given this code, how many major ticks will the secondary y-axis have by default?
Matplotlib
import matplotlib.pyplot as plt x = [0, 1, 2, 3] y1 = [1, 4, 9, 16] y2 = [10, 40, 90, 160] fig, ax1 = plt.subplots() ax1.plot(x, y1) ax2 = ax1.twinx() ax2.plot(x, y2) print(len(ax2.get_yticks()))
Attempts:
2 left
💡 Hint
Check the default number of yticks matplotlib creates for a linear scale.
✗ Incorrect
Matplotlib by default creates 7 major ticks on a linear axis unless customized.
❓ visualization
advanced2:30remaining
Identify the correct secondary x-axis transformation
Which option correctly creates a secondary x-axis that shows the square root of the primary x-axis values?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 16, 100) y = np.sqrt(x) fig, ax = plt.subplots() ax.plot(x, y) secax = ax.secondary_xaxis(1.0, functions=(np.sqrt, np.square)) secax.set_xlabel('Square root of x') plt.show()
Attempts:
2 left
💡 Hint
The first function converts primary to secondary axis, the second converts back.
✗ Incorrect
The secondary_xaxis method takes two functions: forward and inverse. To show square root on secondary axis, forward is sqrt, inverse is square.
🔧 Debug
advanced2:00remaining
Error when adding secondary y-axis with incompatible scales
What error will this code raise when trying to plot on a secondary y-axis with incompatible data types?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [10, 20, 30] y2 = ['a', 'b', 'c'] fig, ax1 = plt.subplots() ax1.plot(x, y1) ax2 = ax1.twinx() ax2.plot(x, y2) plt.show()
Attempts:
2 left
💡 Hint
Check if matplotlib can plot strings as y-values.
✗ Incorrect
Matplotlib cannot convert string values to floats for plotting numeric axes, so it raises a TypeError.
🚀 Application
expert3:00remaining
Using secondary y-axis to compare different units
You have temperature data in Celsius and Fahrenheit for the same dates. Which code snippet correctly plots Celsius on the left y-axis and Fahrenheit on the right y-axis using secondary y-axis?
Matplotlib
import matplotlib.pyplot as plt dates = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] celsius = [0, 10, 20, 15, 5] fahrenheit = [32, 50, 68, 59, 41] fig, ax1 = plt.subplots() ax1.plot(dates, celsius, 'r-', label='Celsius') ax2 = ax1.twinx() ax2.plot(dates, fahrenheit, 'b--', label='Fahrenheit') ax1.set_ylabel('Celsius') ax2.set_ylabel('Fahrenheit') plt.show()
Attempts:
2 left
💡 Hint
Matplotlib can plot categorical x-axis labels as strings.
✗ Incorrect
Matplotlib supports string x-axis labels and twinx() creates a secondary y-axis. The code correctly plots Celsius and Fahrenheit on separate y-axes.