0
0
Matplotlibdata~10 mins

Secondary axes in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Secondary axes
Create main plot
Add secondary axis
Define transform functions
Plot data on secondary axis
Show combined plot
First, create a main plot. Then add a secondary axis with transformation functions. Plot data on both axes and display the combined figure.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
secax = ax.secondary_yaxis('right', functions=(lambda x: x/2, lambda x: x*2))
secax.set_ylabel('Secondary axis')
plt.show()
This code plots data on the main y-axis and adds a secondary y-axis on the right with a scaling transform.
Execution Table
StepActionMain axis dataSecondary axis transformSecondary axis labelPlot shown
1Create figure and main axisEmptynullnullNo
2Plot data on main axis[0,1,4]nullnullNo
3Add secondary y-axis on right[0,1,4]Forward: x/2, Inverse: x*2nullNo
4Set label for secondary axis[0,1,4]Forward: x/2, Inverse: x*2Secondary axisNo
5Show plot with both axes[0,1,4]Forward: x/2, Inverse: x*2Secondary axisYes
💡 Plot displayed with main and secondary y-axes after step 5
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
fignullFigure object createdFigure object createdFigure object createdFigure object created
axnullAxes object createdAxes object createdAxes object createdAxes object created
secaxnullnullSecondaryAxis object createdSecondaryAxis object createdSecondaryAxis object created
main_datanull[0,1,4][0,1,4][0,1,4][0,1,4]
secax_labelnullnullnull'Secondary axis''Secondary axis'
Key Moments - 2 Insights
Why do we need two functions when adding a secondary axis?
The secondary axis needs a forward function to convert main axis values to secondary axis values, and an inverse function to convert back. This ensures correct scaling and interaction, as shown in step 3 of the execution_table.
Does plotting on the main axis automatically update the secondary axis?
No, the secondary axis is linked by the transform functions but does not plot data itself unless explicitly plotted. Step 2 shows main axis plotting, step 3 adds the secondary axis without plotting data on it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the inverse transform function for the secondary axis?
Ax / 2
Bx + 2
Cx * 2
Dx - 2
💡 Hint
Check the 'Secondary axis transform' column at step 3 in the execution_table.
At which step is the secondary axis label set?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Secondary axis label' column in the execution_table.
If we remove the inverse function in step 3, what happens to the secondary axis?
AIt will work the same as before
BIt will not display correctly or may cause errors
CThe main axis data will change
DThe secondary axis label will disappear
💡 Hint
Inverse function is needed for proper scaling as explained in key_moments and shown in step 3.
Concept Snapshot
Secondary axes in matplotlib allow adding an extra axis sharing the same x or y axis.
Use ax.secondary_xaxis or ax.secondary_yaxis with two functions: forward and inverse transforms.
Plot main data on the original axis; secondary axis shows transformed scale.
Set labels on secondary axis for clarity.
Useful for showing data in different units or scales side-by-side.
Full Transcript
This visual execution shows how to add a secondary axis in matplotlib. First, a figure and main axis are created. Then data is plotted on the main axis. Next, a secondary y-axis is added on the right side with two transform functions: one to convert main axis values to secondary axis values, and one to convert back. The secondary axis label is set. Finally, the plot is displayed showing both axes. The key point is that the secondary axis uses transform functions to link scales correctly. The execution table tracks each step and variable changes. The quiz tests understanding of transform functions, labeling, and the role of inverse functions.