0
0
Matplotlibdata~20 mins

Why multiple plots per figure matter in Matplotlib - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Plots Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code with multiple subplots?

Look at the code below that creates two plots in one figure using matplotlib. What will be the shape of the axes object?

Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2)
print(type(axes))
print(axes.shape)
A<class 'numpy.ndarray'>\n(2,)
B<class 'numpy.ndarray'>\n(1, 2)
C<class 'list'>\n(2,)
D<class 'matplotlib.axes._subplots.AxesSubplot'>\n()
Attempts:
2 left
💡 Hint

When you create multiple subplots in one row and multiple columns, axes is a 1D array with shape (2,).

data_output
intermediate
2:00remaining
How many lines are plotted in this figure with multiple subplots?

This code creates a figure with 2 rows and 1 column of plots. Each subplot plots two lines. How many lines are drawn in total?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, axes = plt.subplots(2, 1)
for ax in axes:
    ax.plot(x, np.sin(x))
    ax.plot(x, np.cos(x))
print(sum(len(ax.lines) for ax in axes))
A2
B1
C4
D3
Attempts:
2 left
💡 Hint

Count how many lines each subplot has, then multiply by the number of subplots.

visualization
advanced
2:00remaining
Which option correctly creates a figure with 3 plots arranged in one row?

Choose the code that creates a figure with 3 side-by-side plots using matplotlib. The plots should share the same y-axis.

Afig, axes = plt.subplots(3, 1, sharex=True)
Bfig, axes = plt.subplots(3, 1, sharey=True)
Cfig, axes = plt.subplots(1, 3, sharex=True)
Dfig, axes = plt.subplots(1, 3, sharey=True)
Attempts:
2 left
💡 Hint

Remember, rows come first, then columns in plt.subplots(rows, columns).

🔧 Debug
advanced
2:00remaining
What error does this code raise when creating multiple plots?

What error will this code raise?

import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
axes.plot([1, 2, 3], [4, 5, 6])
AAttributeError: 'numpy.ndarray' object has no attribute 'plot'
BTypeError: plot() missing 1 required positional argument
CValueError: too many values to unpack
DNo error, plots successfully
Attempts:
2 left
💡 Hint

Remember that axes is an array of subplots, not a single subplot.

🚀 Application
expert
2:00remaining
Why use multiple plots per figure in data analysis?

Which of the following is the best reason to use multiple plots in one figure?

ATo compare different data sets side-by-side easily and spot patterns or differences
BTo reduce the total number of figures saved on disk
CTo make the code run faster by plotting multiple graphs at once
DTo avoid using legends in plots
Attempts:
2 left
💡 Hint

Think about how seeing plots together helps understanding data.