0
0
Matplotlibdata~30 mins

Shared axes between subplots in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Shared axes between subplots
📖 Scenario: You are analyzing sales data for two different products over a week. You want to compare their daily sales side by side using charts that share the same vertical scale to make comparison easier.
🎯 Goal: Create two line charts side by side using matplotlib that share the same y-axis. This will help you visually compare the sales trends of the two products on the same scale.
📋 What You'll Learn
Create a dictionary called sales_data with daily sales for two products.
Create a variable called days listing the days of the week.
Create two subplots side by side that share the y-axis.
Plot sales for each product on its own subplot.
Add titles to each subplot and label the x-axis with days.
💡 Why This Matters
🌍 Real World
Sharing axes between subplots is useful when comparing related data sets visually, such as sales trends, temperature changes, or stock prices over time.
💼 Career
Data scientists and analysts often create shared-axis plots to make comparisons clearer and to present data insights effectively to stakeholders.
Progress0 / 4 steps
1
Create the sales data and days list
Create a dictionary called sales_data with these exact entries: 'Product A': [10, 15, 13, 17, 20, 22, 19] and 'Product B': [12, 14, 16, 15, 18, 21, 20]. Also create a list called days with these exact values: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'].
Matplotlib
Need a hint?

Use a dictionary for sales_data with two keys and lists of numbers as values. Use a list for days.

2
Create subplots with shared y-axis
Import matplotlib.pyplot as plt. Then create two subplots side by side using plt.subplots with nrows=1, ncols=2, and sharey=True. Store the result in variables fig and axes.
Matplotlib
Need a hint?

Use plt.subplots(nrows=1, ncols=2, sharey=True) to create the subplots.

3
Plot sales data on each subplot
Use axes[0].plot(days, sales_data['Product A']) to plot Product A sales on the first subplot. Use axes[1].plot(days, sales_data['Product B']) to plot Product B sales on the second subplot. Add titles to each subplot with axes[0].set_title('Product A Sales') and axes[1].set_title('Product B Sales'). Label the x-axis on both subplots with axes[0].set_xlabel('Day') and axes[1].set_xlabel('Day').
Matplotlib
Need a hint?

Use axes[0] and axes[1] to plot and label each subplot separately.

4
Display the plot
Use plt.show() to display the figure with the two subplots.
Matplotlib
Need a hint?

Call plt.show() to display the figure.