0
0
Matplotlibdata~20 mins

Storytelling with visualization sequence in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Visualization Storyteller
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 matplotlib code sequence?

Consider the following Python code using matplotlib to create a sequence of plots. What will be the output shown after running this code?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.title('Sine Wave')
plt.show()

plt.plot(x, np.cos(x))
plt.title('Cosine Wave')
plt.show()
ATwo separate plots: first a sine wave, then a cosine wave, each with their own title.
BAn error occurs because plt.show() is called twice.
COnly the sine wave plot is shown; the cosine plot is not displayed.
DOne plot showing both sine and cosine waves together with the title 'Cosine Wave'.
Attempts:
2 left
💡 Hint

Think about what plt.show() does in matplotlib and how it affects the display of plots.

data_output
intermediate
2:00remaining
How many data points are plotted in this sequence?

Given the code below, how many points are plotted in total across all plots?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 5, 50)

plt.plot(x, x**2)
plt.show()

plt.plot(x, np.sqrt(x))
plt.show()
A25 points in total (split between two plots).
B50 points in total (all in one plot).
C50 points in total (25 in each plot).
D100 points in total (50 in each plot).
Attempts:
2 left
💡 Hint

Check how many points are generated by np.linspace and how many plots are created.

visualization
advanced
2:00remaining
Identify the correct sequence of plots for storytelling

You want to tell a story showing data trends over time using three plots: a line plot, a bar chart, and a scatter plot. Which sequence best supports a clear storytelling flow?

AScatter plot → Bar chart → Line plot
BLine plot → Bar chart → Scatter plot
CLine plot → Scatter plot → Bar chart
DBar chart → Line plot → Scatter plot
Attempts:
2 left
💡 Hint

Think about introducing the trend first, then details, then individual points.

🔧 Debug
advanced
2:00remaining
What error does this matplotlib code raise?

Examine the code below. What error will it raise when run?

Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5])
plt.show()
AValueError: x and y must have same first dimension
BTypeError: 'list' object is not callable
CIndexError: list index out of range
DNo error; plot displays correctly
Attempts:
2 left
💡 Hint

Check if the x and y data lists have the same length.

🚀 Application
expert
3:00remaining
Which option produces a multi-plot storytelling figure with shared x-axis?

Which code snippet correctly creates a figure with 3 vertically stacked plots sharing the same x-axis for storytelling?

A
fig, axs = plt.subplots(3, 1)
axs[0].plot([1,2,3], [1,4,9])
axs[1].bar([1,2,3], [3,2,1])
axs[2].scatter([1,2,3], [2,3,1])
plt.show()
B
fig, axs = plt.subplots(1, 3, sharey=True)
axs[0].plot([1,2,3], [1,4,9])
axs[1].bar([1,2,3], [3,2,1])
axs[2].scatter([1,2,3], [2,3,1])
plt.show()
C
fig, axs = plt.subplots(3, 1, sharex=True)
axs[0].plot([1,2,3], [1,4,9])
axs[1].bar([1,2,3], [3,2,1])
axs[2].scatter([1,2,3], [2,3,1])
plt.show()
D
fig, axs = plt.subplots(3, 1, sharey=True)
axs[0].plot([1,2,3], [1,4,9])
axs[1].bar([1,2,3], [3,2,1])
axs[2].scatter([1,2,3], [2,3,1])
plt.show()
Attempts:
2 left
💡 Hint

Consider the layout and which axis should be shared for aligned storytelling.