0
0
Matplotlibdata~30 mins

Secondary axes in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Plotting with Secondary Axes in Matplotlib
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use plt.show() to display the plot window.