Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using multiple plots in a storytelling visualization sequence?
easy
A. To make the plot colors more vibrant
B. To reduce the size of the data
C. To break data into parts and explain it step-by-step
D. To avoid using titles and labels

Solution

  1. Step 1: Understand storytelling with visualization

    Storytelling with visualization means showing data in parts to explain it clearly.
  2. Step 2: Purpose of multiple plots

    Using multiple plots helps break the data into smaller pieces to tell a clear story step-by-step.
  3. Final Answer:

    To break data into parts and explain it step-by-step -> Option C
  4. Quick Check:

    Storytelling = breaking data into parts [OK]
Hint: Multiple plots show data in steps for clear explanation [OK]
Common Mistakes:
  • Thinking colors are the main reason for multiple plots
  • Believing multiple plots reduce data size
  • Ignoring the importance of titles and labels
2. Which of the following is the correct way to create two plots side by side using matplotlib?
easy
A. plt.subplot(2, 1, 1) and plt.subplot(2, 1, 3)
B. plt.subplot(1, 2, 1) and plt.subplot(1, 2, 2)
C. plt.subplot(1, 1, 1) and plt.subplot(1, 1, 2)
D. plt.subplot(3, 1, 1) and plt.subplot(3, 1, 2)

Solution

  1. Step 1: Understand plt.subplot parameters

    plt.subplot(rows, columns, plot_number) arranges plots in a grid.
  2. Step 2: Create two side-by-side plots

    One row and two columns means plt.subplot(1, 2, 1) and plt.subplot(1, 2, 2) for two plots side by side.
  3. Final Answer:

    plt.subplot(1, 2, 1) and plt.subplot(1, 2, 2) -> Option B
  4. Quick Check:

    One row, two columns = plt.subplot(1, 2, x) [OK]
Hint: Use plt.subplot(1, 2, x) for two side-by-side plots [OK]
Common Mistakes:
  • Using wrong plot numbers like 3 in a 2-plot layout
  • Mixing rows and columns incorrectly
  • Trying to create more plots than grid allows
3. What will be the output arrangement of the following code?
import matplotlib.pyplot as plt
plt.subplot(2, 1, 1)
plt.title('Top Plot')
plt.subplot(2, 1, 2)
plt.title('Bottom Plot')
plt.show()
medium
A. Error because plt.title() is used twice
B. Two plots side by side with titles 'Top Plot' and 'Bottom Plot'
C. One plot with both titles overlapping
D. Two plots stacked vertically with titles 'Top Plot' and 'Bottom Plot'

Solution

  1. Step 1: Understand plt.subplot(2, 1, x)

    This creates 2 rows and 1 column, stacking plots vertically.
  2. Step 2: Titles assigned to each subplot

    First plot gets 'Top Plot', second gets 'Bottom Plot', shown stacked vertically.
  3. Final Answer:

    Two plots stacked vertically with titles 'Top Plot' and 'Bottom Plot' -> Option D
  4. Quick Check:

    2 rows, 1 column = vertical stack [OK]
Hint: Rows first, columns second in plt.subplot for layout [OK]
Common Mistakes:
  • Thinking plots are side by side with (2,1,x)
  • Assuming plt.title() causes error if used twice
  • Expecting one plot instead of two
4. Identify the error in this code that tries to create a 2x2 grid of plots:
import matplotlib.pyplot as plt
plt.subplot(2, 2, 1)
plt.plot([1,2,3])
plt.subplot(2, 2, 5)
plt.plot([3,2,1])
plt.show()
medium
A. Using subplot number 5 in a 2x2 grid causes an error
B. plt.plot() cannot be used inside subplot
C. Missing plt.figure() before subplots
D. No error, code runs fine

Solution

  1. Step 1: Understand subplot numbering in 2x2 grid

    2 rows and 2 columns means subplot numbers 1 to 4 only.
  2. Step 2: Check subplot number 5 usage

    Using subplot(2, 2, 5) is invalid and causes an error.
  3. Final Answer:

    Using subplot number 5 in a 2x2 grid causes an error -> Option A
  4. Quick Check:

    Max subplot number = rows*columns = 4 [OK]
Hint: Subplot number must be ≤ rowsxcolumns [OK]
Common Mistakes:
  • Thinking plt.plot() can't be inside subplot
  • Believing plt.figure() is mandatory before subplots
  • Ignoring subplot numbering limits
5. You want to tell a story showing sales growth over 3 years with separate plots for each year. Which approach best helps your audience understand the story clearly?
hard
A. Create 3 subplots in one column using plt.subplot(3, 1, x) with clear titles and labels
B. Plot all years on one plot without labels
C. Create 1 subplot and plot only the last year's data
D. Use plt.subplot(1, 3, x) but skip titles and labels

Solution

  1. Step 1: Choose subplot layout for storytelling

    Using 3 rows and 1 column (plt.subplot(3, 1, x)) stacks plots vertically, showing each year clearly.
  2. Step 2: Importance of titles and labels

    Clear titles and labels help the audience understand each year's data easily.
  3. Final Answer:

    Create 3 subplots in one column using plt.subplot(3, 1, x) with clear titles and labels -> Option A
  4. Quick Check:

    Separate plots + clear labels = better storytelling [OK]
Hint: Stack plots vertically with titles for clear story [OK]
Common Mistakes:
  • Plotting all data in one plot without labels
  • Skipping titles and labels reduces clarity
  • Showing only last year's data misses story