0
0
Matplotlibdata~30 mins

Why multiple plots per figure matter in Matplotlib - See It in Action

Choose your learning style9 modes available
Why Multiple Plots Per Figure Matter
📖 Scenario: Imagine you are a data analyst working for a small bakery. You want to compare the sales of different types of bread over a week. Instead of making separate charts for each bread type, you want to put all sales charts in one figure to see the differences side by side easily.
🎯 Goal: You will create a figure with multiple plots (subplots) using matplotlib. This will help you compare sales data for different bread types in one place.
📋 What You'll Learn
Create a dictionary called sales_data with bread types as keys and a list of 7 daily sales numbers as values.
Create a variable called days that holds a list of strings representing the days of the week.
Use matplotlib.pyplot.subplots() to create a figure with 3 subplots arranged in one row.
Plot the sales data for each bread type on its own subplot with appropriate titles.
Display the figure with all subplots visible.
💡 Why This Matters
🌍 Real World
Data analysts often need to compare multiple related datasets side by side to find patterns or differences quickly.
💼 Career
Creating multiple plots per figure is a common skill for data visualization roles, helping communicate insights clearly to teams and clients.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'Sourdough': [20, 22, 19, 24, 25, 23, 21], 'Baguette': [15, 17, 14, 16, 18, 17, 16], and 'Rye': [10, 12, 11, 13, 14, 12, 11].
Matplotlib
Need a hint?

Use curly braces {} to create a dictionary. Each bread type is a key with a list of 7 numbers as its value.

2
Create the days list
Create a list called days with these exact strings: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'.
Matplotlib
Need a hint?

Use square brackets [] to create a list of day names as strings.

3
Create subplots and plot sales data
Import matplotlib.pyplot as plt. Use plt.subplots() to create a figure and 3 subplots in one row. Use a for loop with variables ax, (bread, sales) to plot each bread's sales on its subplot. Set the title of each subplot to the bread type.
Matplotlib
Need a hint?

Use plt.subplots(1, 3) to create one row with three plots. Use zip(axes, sales_data.items()) to loop through axes and data together.

4
Display the figure with all subplots
Use plt.tight_layout() to adjust spacing and then plt.show() to display the figure with all three subplots.
Matplotlib
Need a hint?

Use plt.tight_layout() before plt.show() to make sure the plots do not overlap.