📖 Scenario: You are analyzing the sales and profit data of a small store. You want to visualize both sales and profit trends on the same plot but with different scales.
🎯 Goal: Create a line plot with sales on the primary y-axis and profit on a secondary y-axis using Matplotlib.
📋 What You'll Learn
Create a dictionary called data with keys 'Month', 'Sales', and 'Profit' and the exact values provided.
Create a variable called months that holds the list of months from the data dictionary.
Create a Matplotlib plot with sales data on the primary y-axis and profit data on a secondary y-axis.
Label both y-axes and the x-axis correctly.
Display the plot.
💡 Why This Matters
🌍 Real World
Visualizing two related but differently scaled data series on the same plot helps businesses understand relationships like sales and profit trends over time.
💼 Career
Data analysts and scientists often use secondary axes in reports and dashboards to present multiple metrics clearly and effectively.
Progress0 / 4 steps
1
Create the data dictionary
Create a dictionary called data with these exact entries: 'Month' as ["Jan", "Feb", "Mar", "Apr", "May"], 'Sales' as [250, 300, 280, 350, 400], and 'Profit' as [50, 60, 55, 70, 80].
Matplotlib
Hint
Use a dictionary with keys 'Month', 'Sales', and 'Profit'. Assign the exact lists as values.
2
Extract the months list
Create a variable called months that holds the list of months from the data dictionary using the key 'Month'.
Matplotlib
Hint
Use months = data['Month'] to get the list of months.
3
Create the plot with secondary y-axis
Import matplotlib.pyplot as plt. Create a figure and axis using plt.subplots(). Plot data['Sales'] against months on the primary y-axis with label 'Sales'. Create a secondary y-axis using ax.twinx(). Plot data['Profit'] against months on the secondary y-axis with label 'Profit'. Label the x-axis as 'Month'.
Matplotlib
Hint
Use plt.subplots() to create the plot. Use ax.twinx() to create the secondary y-axis.
4
Display the plot
Add a plt.show() statement to display the plot.
Matplotlib
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. What is the main purpose of using secondary_xaxis or secondary_yaxis in matplotlib?
easy
A. To add a new axis with a different scale on the same plot
B. To create a separate figure window
C. To change the color of the existing axis
D. To remove the original axis from the plot
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 secondary_xaxis or secondary_yaxis
These functions add a secondary axis to the existing plot to compare data with different units clearly.
Final Answer:
To add a new axis with a different scale on the same plot -> Option A
Quick Check:
Secondary axes = different scale on same plot [OK]
Hint: Secondary axes add a new scale on the same plot [OK]
Common Mistakes:
Thinking it creates a new figure
Confusing axis color change with adding axes
Assuming it removes original axes
2. Which of the following is the correct way to add a secondary y-axis in matplotlib?
easy
A. ax.add_secondary_yaxis('right', functions=(forward, inverse))
B. ax.add_secondary_axis('right', functions=(forward, inverse))
C. ax.secondary_yaxis('right', functions=(forward, inverse))
D. ax.secondary_axis('right', functions=(forward, inverse))
Solution
Step 1: Recall the matplotlib method for secondary y-axis
The correct method is secondary_yaxis called on the axes object.
Step 2: Match the exact syntax
The method is ax.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 C
Quick Check:
Correct method name = secondary_yaxis [OK]
Hint: Use ax.secondary_yaxis with conversion functions [OK]
Common Mistakes:
Using incorrect method names like add_secondary_axis
B. The order of functions in the tuple is reversed
C. The position 'top' is invalid for secondary_xaxis
D. The method name should be secondary_yaxis instead
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 B
Quick Check:
Functions order = (forward, inverse) [OK]
Hint: First function forward, second inverse in tuple [OK]
Common Mistakes:
Swapping forward and inverse functions
Using invalid axis position
Confusing secondary_xaxis with secondary_yaxis
5. You have temperature data in Celsius and want to add a secondary y-axis in Fahrenheit on the right side. Which of these code snippets correctly creates the secondary axis with proper conversion?