0
0
Matplotlibdata~10 mins

Storytelling with visualization sequence in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Storytelling with visualization sequence
Start: Define data
Create first plot
Show plot to audience
Create second plot
Show second plot
Add narrative text
Repeat for more plots
End: Complete story
This flow shows how to build a story by creating and showing plots one after another, adding text to guide the audience.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
y2 = [30, 25, 20, 10]

plt.plot(x, y1)
plt.title('First plot')
plt.show()

plt.plot(x, y2)
plt.title('Second plot')
plt.show()
This code creates two line plots one after another to tell a simple story with data.
Execution Table
StepActionPlot CreatedPlot TitleOutput
1Define x and y1 dataNoN/AData ready
2Plot x vs y1YesFirst plotLine plot with y1 values
3Show first plotYesFirst plotPlot displayed to user
4Plot x vs y2YesSecond plotLine plot with y2 values
5Show second plotYesSecond plotPlot displayed to user
6End of sequenceNoN/AStory complete
💡 All plots created and shown, storytelling sequence complete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
xundefined[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
y1undefined[10, 20, 25, 30][10, 20, 25, 30][10, 20, 25, 30][10, 20, 25, 30]
y2undefinedundefinedundefined[30, 25, 20, 10][30, 25, 20, 10]
plotnonenoneplot with y1plot with y2plot with y2
Key Moments - 2 Insights
Why do we call plt.show() after each plot?
Calling plt.show() displays the current plot and pauses the code until the window is closed, so each plot appears separately as shown in steps 3 and 5.
What happens if we plot multiple lines without calling plt.show() in between?
Without plt.show() between plots, all lines appear on the same figure, not as separate plots. The execution_table shows separate plots because plt.show() is called after each.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the plot title at Step 4?
AFirst plot
BNo title
CSecond plot
DUndefined
💡 Hint
Check the 'Plot Title' column for Step 4 in the execution_table.
At which step is the first plot shown to the user?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Output' columns in the execution_table for when the plot is displayed.
If we remove plt.show() after the first plot, what changes in the execution?
ABoth plots appear on the same figure
BNo plots appear
COnly the first plot appears
DPlots appear in reverse order
💡 Hint
Refer to the key moment about plt.show() and how it separates plots.
Concept Snapshot
Storytelling with visualization sequence:
- Prepare data
- Create a plot
- Call plt.show() to display it
- Add narrative or next plot
- Repeat for multiple visuals
- Each plt.show() pauses and shows current plot
- Helps guide audience step-by-step
Full Transcript
This lesson shows how to tell a story using a sequence of visualizations with matplotlib. We start by defining data arrays. Then we create the first plot and call plt.show() to display it. This pauses the code and shows the plot to the audience. Next, we create a second plot and show it again. Each plot is shown separately, helping the audience focus on one part of the story at a time. Calling plt.show() after each plot is important to separate visuals. Without it, all lines would appear on the same figure. This step-by-step approach helps make data stories clear and engaging.