Bird
Raised Fist0
Pandasdata~30 mins

Subplots for multiple charts in Pandas - Mini Project: Build & Apply

Choose your learning style10 modes available
Subplots for multiple charts
📖 Scenario: You work as a data analyst for a small store. You have sales data for three products over four months. You want to see how each product's sales changed over time by drawing separate line charts side by side.
🎯 Goal: Create a pandas DataFrame with sales data, set up a configuration for the number of subplots, plot each product's sales as a separate line chart using subplots, and display the charts.
📋 What You'll Learn
Create a pandas DataFrame with sales data for three products over four months.
Create a variable to store the number of subplots (one for each product).
Use matplotlib subplots to create separate line charts for each product's sales.
Display the subplots with clear titles and labels.
💡 Why This Matters
🌍 Real World
Stores and businesses often track sales of different products over time. Visualizing each product's sales separately helps spot trends and compare performance clearly.
💼 Career
Data analysts and scientists use subplots to present multiple related charts in reports and dashboards, making data easier to understand for decision makers.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with the following data: months as the index with values 'Jan', 'Feb', 'Mar', 'Apr', and three columns 'Product_A', 'Product_B', 'Product_C' with sales values: [10, 15, 20, 25], [5, 7, 9, 11], and [12, 14, 16, 18] respectively.
Pandas
Hint

Use pd.DataFrame with a dictionary for columns and index for months.

2
Set the number of subplots
Create a variable called num_plots and set it to the number of columns in sales_data.
Pandas
Hint

Use sales_data.shape[1] to get the number of columns.

3
Create subplots and plot each product's sales
Import matplotlib.pyplot as plt. Use plt.subplots with num_plots rows and 1 column to create subplots. Use a for loop with variables idx and product to iterate over enumerate(sales_data.columns). Inside the loop, plot the sales data for each product on the corresponding subplot using axes[idx].plot. Set the title of each subplot to the product name using axes[idx].set_title(product). Label the x-axis as 'Month' and y-axis as 'Sales' for each subplot.
Pandas
Hint

Use plt.subplots to create multiple plots and loop over columns with enumerate.

4
Display the subplots
Add a line to display the plots using plt.show().
Pandas
Hint

Use plt.show() to display the figure window with all subplots.

Practice

(1/5)
1. What is the main purpose of using subplots in pandas plotting?
easy
A. To display multiple charts in one figure for easy comparison
B. To save plots as separate image files automatically
C. To change the color of a single plot
D. To increase the size of a single chart

Solution

  1. Step 1: Understand what subplots do

    Subplots allow multiple charts to be shown together in one figure.
  2. Step 2: Compare options

    Only To display multiple charts in one figure for easy comparison describes this purpose correctly.
  3. Final Answer:

    To display multiple charts in one figure for easy comparison -> Option A
  4. Quick Check:

    Subplots = multiple charts in one figure [OK]
Hint: Subplots = multiple charts in one figure [OK]
Common Mistakes:
  • Thinking subplots save images automatically
  • Confusing subplots with styling options
  • Believing subplots only affect one chart
2. Which of the following is the correct way to create a 2x2 grid of subplots using matplotlib with pandas?
easy
A. fig, axes = plt.subplots(rows=2)
B. fig, axes = plt.subplots(4)
C. fig, axes = plt.subplots(columns=2)
D. fig, axes = plt.subplots(2, 2)

Solution

  1. Step 1: Recall plt.subplots syntax

    The function plt.subplots(rows, columns) creates a grid of plots.
  2. Step 2: Match the 2x2 grid

    Using plt.subplots(2, 2) creates 2 rows and 2 columns, making 4 subplots.
  3. Final Answer:

    fig, axes = plt.subplots(2, 2) -> Option D
  4. Quick Check:

    plt.subplots(2, 2) = 2 rows, 2 columns [OK]
Hint: Use plt.subplots(rows, columns) for grid size [OK]
Common Mistakes:
  • Using plt.subplots(4) creates 4 rows, not a 2x2 grid
  • Using keyword arguments incorrectly
  • Confusing rows and columns parameters
3. Given the code below, what will axes be?
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 3)
print(type(axes))
medium
A. <class 'numpy.ndarray'>
B. <class 'matplotlib.axes._subplots.AxesSubplot'>
C. <class 'list'>
D. <class 'tuple'>

Solution

  1. Step 1: Understand plt.subplots output for multiple plots

    When creating multiple subplots in one row, axes is a numpy array of AxesSubplot objects.
  2. Step 2: Check the printed type

    Printing type(axes) shows numpy.ndarray because axes is an array of subplot axes.
  3. Final Answer:

    <class 'numpy.ndarray'> -> Option A
  4. Quick Check:

    Multiple subplots = axes is numpy array [OK]
Hint: Multiple subplots return numpy array of axes [OK]
Common Mistakes:
  • Thinking axes is a single AxesSubplot object
  • Assuming axes is a list or tuple
  • Confusing axes with figure object
4. What is wrong with this code snippet for creating subplots?
fig, axes = plt.subplots(1)
axes[0].plot([1, 2, 3])
axes[1].plot([4, 5, 6])
medium
A. plt.subplots needs two arguments for multiple plots
B. axes is a single AxesSubplot, so indexing causes an error
C. axes is not iterable when only one argument is given
D. plot data lists must be the same length

Solution

  1. Step 1: Understand plt.subplots(1) output

    plt.subplots(1) creates a single subplot (1 row, 1 column), so axes is a single AxesSubplot object, not an array.
  2. Step 2: Indexing causes error

    Trying axes[0] or axes[1] on a single AxesSubplot raises TypeError: 'AxesSubplot' object is not subscriptable.
  3. Final Answer:

    axes is a single AxesSubplot, so indexing causes an error -> Option B
  4. Quick Check:

    plt.subplots(1) = single axes, indexing fails [OK]
Hint: Check if axes is array before indexing [OK]
Common Mistakes:
  • Assuming axes is always a single plot
  • Forgetting axes can be array for multiple subplots
  • Confusing number of arguments with subplot shape
5. You want to plot three different pandas DataFrame columns side-by-side using subplots in one row. Which code correctly creates the subplots and plots each column separately?
hard
A. fig, axes = plt.subplots(3, 1) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i])
B. fig, axes = plt.subplots(3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i])
C. fig, axes = plt.subplots(1, 3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i])
D. fig, axes = plt.subplots(1, 3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=fig[i])

Solution

  1. Step 1: Determine subplot layout for side-by-side plots

    Side-by-side means 1 row and 3 columns, so plt.subplots(1, 3) is correct.
  2. Step 2: Check plotting loop and axes usage

    Looping over columns and plotting on axes[i] is correct usage.
  3. Step 3: Identify errors in other options

    fig, axes = plt.subplots(3, 1) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i]) uses (3,1) which is vertical layout, not side-by-side. fig, axes = plt.subplots(1, 3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=fig[i]) uses fig[i] which is invalid. fig, axes = plt.subplots(3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i]) uses plt.subplots(3) which creates 3 rows and 1 column, not side-by-side.
  4. Final Answer:

    fig, axes = plt.subplots(1, 3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i]) -> Option C
  5. Quick Check:

    Side-by-side = plt.subplots(1, 3) + axes[i] [OK]
Hint: Side-by-side = 1 row, multiple columns in plt.subplots [OK]
Common Mistakes:
  • Using wrong subplot shape for layout
  • Passing fig instead of axes to plot
  • Confusing rows and columns in plt.subplots
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard