Bird
Raised Fist0
Data Analysis Pythondata~5 mins

Subplots for multiple charts in Data Analysis Python

Choose your learning style10 modes available
Introduction

Subplots let you show many charts in one picture. This helps compare data easily.

You want to compare sales data from different months side by side.
You need to show temperature and rainfall charts for the same city.
You want to display different parts of a dataset in separate small charts.
You are preparing a report and want all charts in one image for easy viewing.
Syntax
Data Analysis Python
import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows, ncols)

axs[row, col].plot(data)
plt.show()

fig is the whole figure holding all charts.

axs is an array of charts you can fill with data.

Examples
Creates 1 row and 2 columns of charts side by side.
Data Analysis Python
fig, axs = plt.subplots(1, 2)
axs[0].plot([1, 2, 3])
axs[1].plot([3, 2, 1])
plt.show()
Creates 2 rows and 2 columns of charts in a grid.
Data Analysis Python
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2])
axs[0, 1].plot([2, 3])
axs[1, 0].plot([3, 4])
axs[1, 1].plot([4, 5])
plt.show()
Sample Program

This code creates a 2x2 grid of charts showing squares, cubes, linear, and reverse squares.

Data Analysis Python
import matplotlib.pyplot as plt

# Create 2 rows and 2 columns of charts
fig, axs = plt.subplots(2, 2)

# Data for each chart
x = [1, 2, 3, 4]

# Plot different lines in each subplot
axs[0, 0].plot(x, [1, 4, 9, 16])
axs[0, 0].set_title('Squares')

axs[0, 1].plot(x, [1, 8, 27, 64])
axs[0, 1].set_title('Cubes')

axs[1, 0].plot(x, [1, 2, 3, 4])
axs[1, 0].set_title('Linear')

axs[1, 1].plot(x, [16, 9, 4, 1])
axs[1, 1].set_title('Reverse Squares')

plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

Use plt.tight_layout() to avoid overlapping titles and labels.

Indexing in axs depends on the shape: 1D array if one row or column, 2D array if multiple rows and columns.

You can add titles and labels to each subplot for clarity.

Summary

Subplots help show many charts in one figure for easy comparison.

Use plt.subplots(rows, cols) to create a grid of charts.

Access each chart with axs[row, col] and plot your data.

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

  1. Step 1: Understand the function of plt.subplots()

    This function creates a figure and a grid of subplots (axes) to hold multiple charts.
  2. Step 2: Recognize the benefit of multiple charts in one figure

    Having multiple charts in one figure helps compare data visually side-by-side.
  3. Final Answer:

    To create 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 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

  1. Step 1: Recall the syntax of plt.subplots()

    The function takes rows and columns as arguments to create a grid.
  2. Step 2: Identify the 2x2 grid syntax

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

    fig, axs = plt.subplots(2, 2) -> Option D
  4. 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)?
medium
A. A list of 3 Axes objects
B. A single Axes object
C. A 3x1 numpy array of Axes objects
D. A 1x3 numpy array of Axes objects

Solution

  1. Step 1: Understand plt.subplots(3, 1) creates 3 rows and 1 column

    This means 3 subplots stacked vertically.
  2. Step 2: Check the type of axs for single column

    When columns = 1, axs is a 2D numpy array of shape (3,1) of Axes objects, not a list.
  3. Final Answer:

    A 3x1 numpy array of Axes objects -> Option C
  4. Quick Check:

    Single column = 2D numpy array of shape (3,1) [OK]
Hint: Single column subplots return 2D numpy array of shape (n,1) [OK]
Common Mistakes:
  • Assuming always 1D list
  • Confusing rows and columns order
  • Expecting single Axes object
4. Identify the error in this code snippet for plotting on subplots:
fig, axs = plt.subplots(2, 2)
axs[2, 0].plot([1, 2, 3])
medium
A. TypeError because axs is not subscriptable
B. IndexError because axs[2, 0] is out of range
C. No error, code runs fine
D. SyntaxError due to wrong indexing

Solution

  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.
  2. Step 2: Analyze the index axs[2, 0]

    Index 2 for rows is out of range (only 0 and 1 allowed), causing IndexError.
  3. Final Answer:

    IndexError because axs[2, 0] is out of range -> Option B
  4. 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

  1. Step 1: Understand subplot grid and number of plots

    A 2x3 grid has 6 subplots but only 5 are needed, so one remains empty.
  2. Step 2: Use axs.flat to iterate all subplots in 1D

    This allows easy indexing over the 2D array of axes.
  3. Step 3: Turn off the extra subplot axis

    Calling axs.flat[5].axis('off') hides the unused subplot.
  4. 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
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard