Secondary axes let you show two different scales on the same plot. This helps compare data that have different units or ranges.
Secondary axes in Matplotlib
ax.secondary_xaxis(location, functions=(forward, inverse)) ax.secondary_yaxis(location, functions=(forward, inverse))
location is where the secondary axis appears (e.g., 'top' or 'right').
functions are two functions: one to convert from primary to secondary axis, and one to convert back.
secax = ax.secondary_yaxis('right')secax = ax.secondary_xaxis('top', functions=(lambda x: x*1.6, lambda x: x/1.6))
This code plots y = x² on the primary y-axis. It adds a secondary y-axis on the right side that shows the values scaled down by 10. This helps compare the same data in a different scale.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y1 = x ** 2 # primary y-axis data def forward(x): return x / 10 # convert primary y to secondary y def inverse(x): return x * 10 # convert secondary y back to primary y fig, ax = plt.subplots() ax.plot(x, y1, label='y = x^2') ax.set_ylabel('Primary Y axis (x^2)') secax = ax.secondary_yaxis('right', functions=(forward, inverse)) secax.set_ylabel('Secondary Y axis (scaled)') ax.set_xlabel('X axis') ax.legend() plt.show()
Secondary axes need conversion functions if scales differ.
Without functions, the secondary axis matches the primary axis scale.
Use secondary axes to make complex data easier to understand.
Secondary axes show two scales on one plot.
Use conversion functions to link primary and secondary axes.
They help compare different units or ranges clearly.
Practice
secondary_xaxis or secondary_yaxis in matplotlib?Solution
Step 1: Understand the concept of secondary axes
Secondary axes allow adding a new axis that shares the same plot but has a different scale or units.Step 2: Identify the purpose of
These functions add a secondary axis to the existing plot to compare data with different units clearly.secondary_xaxisorsecondary_yaxisFinal Answer:
To add a new axis with a different scale on the same plot -> Option AQuick Check:
Secondary axes = different scale on same plot [OK]
- Thinking it creates a new figure
- Confusing axis color change with adding axes
- Assuming it removes original axes
Solution
Step 1: Recall the matplotlib method for secondary y-axis
The correct method issecondary_yaxiscalled on the axes object.Step 2: Match the exact syntax
The method isax.secondary_yaxis('right', functions=(forward, inverse))where 'right' is the position and functions are conversion functions.Final Answer:
ax.secondary_yaxis('right', functions=(forward, inverse)) -> Option CQuick Check:
Correct method name = secondary_yaxis [OK]
- Using incorrect method names like add_secondary_axis
- Missing the functions parameter
- Using secondary_axis instead of secondary_yaxis
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 10, 20])
forward = lambda x: x / 10
inverse = lambda x: x * 10
secax = ax.secondary_yaxis('right', functions=(forward, inverse))
print(secax.get_ylim())Solution
Step 1: Understand the original y-axis limits
The original y-axis data ranges from 0 to 20, so default limits are approximately (0, 20).Step 2: Apply the forward function to original limits for secondary axis
The forward function divides by 10, so 0 becomes 0.0 and 20 becomes 2.0 on the secondary axis.Final Answer:
[0.0, 2.0] -> Option AQuick Check:
Secondary axis limits = original limits / 10 [OK]
- Confusing forward and inverse functions
- Expecting original limits instead of transformed
- Assuming inverse function applies to limits
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
forward = lambda x: x * 2
inverse = lambda x: x / 2
secax = ax.secondary_xaxis('top', functions=(inverse, forward))Solution
Step 1: Check the order of conversion functions
The first function should convert from primary to secondary axis (forward), the second from secondary to primary (inverse).Step 2: Identify the mistake in the code
The code uses (inverse, forward) instead of (forward, inverse), so the order is reversed causing incorrect scaling.Final Answer:
The order of functions in the tuple is reversed -> Option BQuick Check:
Functions order = (forward, inverse) [OK]
- Swapping forward and inverse functions
- Using invalid axis position
- Confusing secondary_xaxis with secondary_yaxis
Solution
Step 1: Recall Celsius to Fahrenheit conversion formulas
Celsius to Fahrenheit: F = (C * 9/5) + 32; Fahrenheit to Celsius: C = (F - 32) * 5/9.Step 2: Match the forward and inverse functions correctly
Forward converts primary (C) to secondary (F), inverse converts secondary (F) back to primary (C). Position 'right' is correct for secondary y-axis.Final Answer:
forward = lambda c: (c * 9/5) + 32 inverse = lambda f: (f - 32) * 5/9 ax.secondary_yaxis('right', functions=(forward, inverse)) -> Option DQuick Check:
Correct conversion and axis position = forward = lambda c: (c * 9/5) + 32 inverse = lambda f: (f - 32) * 5/9 ax.secondary_yaxis('right', functions=(forward, inverse)) [OK]
- Swapping forward and inverse functions
- Using wrong conversion formulas
- Placing secondary axis on 'left' instead of 'right'
Ready to prove it?
Pass the test and this pattern moves unknown
