0
0
Data Analysis Pythondata~20 mins

Subplots for multiple charts in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subplots Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a 2x2 subplot grid with line plots
What is the output of this code that creates a 2x2 grid of line plots with matplotlib?
Data Analysis Python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
fig, axs = plt.subplots(2, 2)
for i, ax in enumerate(axs.flat):
    ax.plot(x, x*(i+1))
plt.tight_layout()
plt.show()
AA single plot with four lines overlapping, slopes 1 to 4.
BFour line plots arranged in 2 rows and 2 columns, each plot showing lines with slopes 1, 2, 3, and 4 respectively.
CAn error because axs.flat is not iterable.
DFour scatter plots arranged in 2 rows and 2 columns.
Attempts:
2 left
💡 Hint
Remember that axs.flat lets you iterate over all subplot axes in a grid.
data_output
intermediate
1:00remaining
Number of subplots created by plt.subplots(3,1)
How many subplot axes are created by the command plt.subplots(3, 1)?
AAn error because 3 and 1 are invalid arguments
B1 subplot axis spanning 3 rows
C6 subplot axes arranged in 3 rows and 2 columns
D3 subplot axes arranged vertically
Attempts:
2 left
💡 Hint
The first argument is rows, the second is columns.
visualization
advanced
2:00remaining
Identify the subplot layout from the code
Given this code, what is the layout of the subplots shown?
Data Analysis Python
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(9, 3))
for i, ax in enumerate(axs):
    ax.bar([1, 2, 3], [i+1, i+2, i+3])
plt.show()
AThree bar charts side by side in one row
BThree bar charts stacked vertically in one column
CA single bar chart with three bars
DAn error because axs is not iterable
Attempts:
2 left
💡 Hint
plt.subplots(1, 3) means 1 row and 3 columns.
🔧 Debug
advanced
1:30remaining
Why does this subplot code raise an error?
What error does this code raise and why? import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) axs[2].plot([1, 2, 3], [4, 5, 6]) plt.show()
AIndexError because axs has shape (2, 2) and index 2 is out of range
BTypeError because axs is not subscriptable
CValueError because plot data lengths differ
DNo error, the plot shows correctly
Attempts:
2 left
💡 Hint
axs is a 2D array of axes, not a flat list.
🚀 Application
expert
3:00remaining
Create a 2x3 grid of scatter plots with different colors
You want to create a 2 rows by 3 columns grid of scatter plots using matplotlib. Each subplot should plot points (x, y) where x = [1,2,3] and y = [3,2,1] multiplied by the subplot index (starting at 1). Each subplot should have a unique color from ['red', 'green', 'blue', 'orange', 'purple', 'brown']. Which code snippet correctly creates this figure?
A
import matplotlib.pyplot as plt
x = [1,2,3]
y = [3,2,1]
colors = ['red', 'green', 'blue', 'orange', 'purple', 'brown']
fig, axs = plt.subplots(2, 3)
for i in range(6):
    axs[i].scatter(x, [v*i for v in y], color=colors[i])
plt.show()
B
import matplotlib.pyplot as plt
x = [1,2,3]
y = [3,2,1]
colors = ['red', 'green', 'blue', 'orange', 'purple', 'brown']
fig, axs = plt.subplots(3, 2)
for i, ax in enumerate(axs.flat):
    ax.scatter(x, [v*i for v in y], color=colors[i])
plt.show()
C
import matplotlib.pyplot as plt
x = [1,2,3]
y = [3,2,1]
colors = ['red', 'green', 'blue', 'orange', 'purple', 'brown']
fig, axs = plt.subplots(2, 3)
for i, ax in enumerate(axs.flat, 1):
    ax.scatter(x, [v*i for v in y], color=colors[i-1])
plt.tight_layout()
plt.show()
D
import matplotlib.pyplot as plt
x = [1,2,3]
y = [3,2,1]
colors = ['red', 'green', 'blue', 'orange', 'purple', 'brown']
fig, axs = plt.subplots(2, 3)
for i, ax in enumerate(axs):
    ax.scatter(x, [v*(i+1) for v in y], color=colors[i])
plt.show()
Attempts:
2 left
💡 Hint
Use axs.flat to iterate over all axes in a 2D grid and start index at 1 for multiplication.