📖 Scenario: You are analyzing sales and profit data for a small store. Sales are in thousands of dollars, and profit is in hundreds of dollars. You want to show both on the same chart but with different y-axis scales.
🎯 Goal: Create a line chart with two y-axes: one for sales and one for profit, so both can be seen clearly despite different scales.
📋 What You'll Learn
Create lists for sales and profit data with exact values
Create a list for months from January to June
Create a plot with two y-axes using matplotlib
Plot sales on the left y-axis and profit on the right y-axis
Label both y-axes and the x-axis
Add a legend to distinguish sales and profit lines
💡 Why This Matters
🌍 Real World
Dual y-axis charts are useful when comparing two related data sets with different units or scales, like sales and profit, temperature and humidity, or stock price and volume.
💼 Career
Data analysts and scientists often need to visualize multiple metrics together to find insights. Knowing how to create dual y-axis plots helps communicate complex data clearly.
Progress0 / 4 steps
1
Create the data lists
Create a list called months with these exact values: 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'. Create a list called sales with these exact values: 20, 35, 30, 35, 27, 40. Create a list called profit with these exact values: 4, 7, 6, 8, 5, 9.
Matplotlib
Hint
Use square brackets to create lists. Separate items with commas.
2
Set up the plot and first y-axis
Import matplotlib.pyplot as plt. Create a figure and axis using fig, ax1 = plt.subplots(). Plot sales against months on ax1 with a blue line and label it 'Sales'. Set the x-axis label to 'Month' and the y-axis label of ax1 to 'Sales (thousands)'.
Matplotlib
Hint
Use plt.subplots() to create the plot. Use ax1.plot() to plot sales.
3
Add the second y-axis and plot profit
Create a second y-axis ax2 using ax1.twinx(). Plot profit against months on ax2 with a red line and label it 'Profit'. Set the y-axis label of ax2 to 'Profit (hundreds)' with red color. Add legends for both ax1 and ax2.
Matplotlib
Hint
Use ax1.twinx() to create the second y-axis. Use different colors for clarity.
4
Display the plot
Use plt.show() to display the plot with dual y-axes.
Matplotlib
Hint
Use plt.show() to see the plot window.
Practice
(1/5)
1. What is the main purpose of using a dual y-axis in a matplotlib plot?
easy
A. To compare two datasets with different scales on the same plot
B. To create two separate plots side by side
C. To add a legend to the plot
D. To change the color of the plot lines
Solution
Step 1: Understand the purpose of dual y-axis
A dual y-axis allows plotting two datasets with different value ranges on the same graph for easy comparison.
Step 2: Identify the correct use case
Comparing datasets with different scales requires two y-axes; other options do not describe this purpose.
Final Answer:
To compare two datasets with different scales on the same plot -> Option A
Quick Check:
Dual y-axis = compare different scales [OK]
Hint: Dual y-axis means two y-scales on one graph [OK]
Common Mistakes:
Thinking dual y-axis creates two separate plots
Confusing dual y-axis with adding legends
Assuming it changes line colors only
2. Which matplotlib function is used to create a second y-axis sharing the same x-axis?
easy
A. plt.subplots()
B. ax.twiny()
C. ax.twinx()
D. plt.plot()
Solution
Step 1: Recall matplotlib methods for axes
The method twinx() creates a second y-axis sharing the x-axis.
Step 2: Differentiate from similar methods
twiny() creates a second x-axis, not y-axis; subplots() creates figure and axes; plot() draws lines.
Final Answer:
ax.twinx() -> Option C
Quick Check:
Second y-axis = twinx() [OK]
Hint: Use ax.twinx() for second y-axis, twiny() is for x-axis [OK]
twinx() is a method of an axes object, not plt module. So plt.twinx() is invalid.
Step 2: Correct method call
Use ax.twinx() to create the second y-axis sharing the x-axis.
Final Answer:
plt.twinx() should be ax.twinx() -> Option D
Quick Check:
twinx() is an axes method, not plt function [OK]
Hint: twinx() must be called on axes object, not plt [OK]
Common Mistakes:
Calling twinx() on plt instead of axes
Confusing figure and axes creation methods
Missing plt.show() (though present here)
5. You want to plot temperature (°C) and rainfall (mm) over months on the same plot with dual y-axes. Which of these code snippets correctly sets different y-axis labels and colors for clarity?
hard
A. ax1.set_xlabel('Temperature (°C)', color='red')\nax2.set_xlabel('Rainfall (mm)', color='blue')
B. ax1.set_ylabel('Temperature (°C)', color='red')\nax2.set_ylabel('Rainfall (mm)', color='blue')
C. ax1.set_ylabel('Rainfall (mm)', color='blue')\nax2.set_ylabel('Temperature (°C)', color='red')
D. ax1.set_title('Temperature and Rainfall')\nax2.set_title('Temperature and Rainfall')
Solution
Step 1: Assign correct y-axis labels
Temperature and rainfall are y-axis data, so use set_ylabel() on each axis with matching labels.
Step 2: Use colors to distinguish axes
Setting label colors helps visually separate the two y-axes for clarity.
Final Answer:
ax1.set_ylabel('Temperature (°C)', color='red')\nax2.set_ylabel('Rainfall (mm)', color='blue') -> Option B