📖 Scenario: You work as a data analyst for a small store. You have sales data for two products over four months. You want to create two charts side by side to compare their sales trends easily.
🎯 Goal: Create two line charts side by side using subplots to show monthly sales for Product A and Product B.
📋 What You'll Learn
Create a dictionary called sales_data with monthly sales for Product A and Product B
Create a variable called months with the list of month names
Use plt.subplots() to create two side-by-side plots
Plot Product A sales on the first subplot and Product B sales on the second subplot
Add titles to each subplot: 'Product A Sales' and 'Product B Sales'
Display the plots side by side
💡 Why This Matters
🌍 Real World
Creating multiple charts side by side helps compare different data sets visually, which is common in business reports and presentations.
💼 Career
Data analysts and scientists often use subplots to present multiple related charts clearly and efficiently.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'Product A': [150, 200, 250, 300] and 'Product B': [180, 210, 230, 280].
Data Analysis Python
Hint
Use curly braces {} to create the dictionary with keys 'Product A' and 'Product B' and their sales lists as values.
2
Create the months list
Create a list called months with these exact values: ['Jan', 'Feb', 'Mar', 'Apr'].
Data Analysis Python
Hint
Use square brackets [] to create the list of months in order.
3
Create subplots and plot the sales data
Import matplotlib.pyplot as plt. Use plt.subplots() to create two side-by-side plots stored in variables fig and axes. Plot months vs sales_data['Product A'] on axes[0] and months vs sales_data['Product B'] on axes[1]. Add titles 'Product A Sales' and 'Product B Sales' to the respective subplots.
Data Analysis Python
Hint
Use plt.subplots(1, 2) to create two plots side by side. Use axes[0].plot() and axes[1].plot() to plot each product's sales. Use set_title() to add titles.
4
Display the subplots
Add a line to display the plots using plt.show().
Data Analysis Python
Hint
Use plt.show() to display the figure with both subplots.
Practice
(1/5)
1. What is the main purpose of using plt.subplots() in data visualization?
easy
A. To create multiple charts in one figure for easy comparison
B. To save a plot as an image file
C. To change the color of a single plot
D. To add a title to a chart
Solution
Step 1: Understand the function of plt.subplots()
This function creates a figure and a grid of subplots (axes) to hold multiple charts.
Step 2: Recognize the benefit of multiple charts in one figure
Having multiple charts in one figure helps compare data visually side-by-side.
Final Answer:
To create multiple charts in one figure for easy comparison -> Option A
Quick Check:
Subplots = multiple charts in one figure [OK]
Hint: Subplots = multiple charts in one figure [OK]
Common Mistakes:
Thinking it saves images
Confusing with styling functions
Assuming it adds titles
2. Which of the following is the correct way to create a 2x2 grid of subplots using Matplotlib?
easy
A. fig, axs = plt.subplots(2)
B. fig, axs = plt.subplots(4)
C. fig, axs = plt.subplots(1, 4)
D. fig, axs = plt.subplots(2, 2)
Solution
Step 1: Recall the syntax of plt.subplots()
The function takes rows and columns as arguments to create a grid.
Step 2: Identify the 2x2 grid syntax
Using plt.subplots(2, 2) creates 2 rows and 2 columns, making 4 subplots.
Final Answer:
fig, axs = plt.subplots(2, 2) -> Option D
Quick Check:
Rows and columns = plt.subplots(2, 2) [OK]
Hint: Rows and columns go as two numbers in plt.subplots() [OK]
Common Mistakes:
Passing only one number for grid
Confusing total plots with grid shape
Using wrong argument order
3. What will be the output shape of axs when you run fig, axs = plt.subplots(3, 1)?
Step 1: Check the shape of axs from plt.subplots(2, 2)
It creates a 2x2 grid, so valid indices are 0 or 1 for rows and columns.
Step 2: Analyze the index axs[2, 0]
Index 2 for rows is out of range (only 0 and 1 allowed), causing IndexError.
Final Answer:
IndexError because axs[2, 0] is out of range -> Option B
Quick Check:
Index out of range = IndexError [OK]
Hint: Check subplot indices start at 0 and max is rows-1 [OK]
Common Mistakes:
Using indices beyond grid size
Confusing syntax error with index error
Assuming axs is a flat list
5. You want to plot 5 different line charts in a single figure using subplots arranged in 2 rows and 3 columns. Which code correctly handles the extra subplot that will remain empty?
hard
A. fig, axs = plt.subplots(2, 3)
for i in range(5):
axs.flat[i].plot([i, i+1, i+2])
axs.flat[5].axis('off')
B. fig, axs = plt.subplots(2, 3)
for i in range(6):
axs[i].plot([i, i+1, i+2])
C. fig, axs = plt.subplots(5, 1)
for i in range(5):
axs[i].plot([i, i+1, i+2])
D. fig, axs = plt.subplots(1, 5)
for i in range(5):
axs[i].plot([i, i+1, i+2])
Solution
Step 1: Understand subplot grid and number of plots
A 2x3 grid has 6 subplots but only 5 are needed, so one remains empty.
Step 2: Use axs.flat to iterate all subplots in 1D
This allows easy indexing over the 2D array of axes.
Step 3: Turn off the extra subplot axis
Calling axs.flat[5].axis('off') hides the unused subplot.
Final Answer:
fig, axs = plt.subplots(2, 3)
for i in range(5):
axs.flat[i].plot([i, i+1, i+2])
axs.flat[5].axis('off') -> Option A
Quick Check:
Use axs.flat and turn off extra subplot [OK]
Hint: Use axs.flat to index subplots and turn off unused ones [OK]
Common Mistakes:
Ignoring extra subplot and leaving it blank
Indexing axs as 1D when it's 2D without flat
Creating wrong grid shape for number of plots
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.