0
0
Matplotlibdata~10 mins

Plotting multiple lines in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Plotting multiple lines
Start matplotlib setup
Create figure and axes
Prepare data for line 1
Plot line 1
Prepare data for line 2
Plot line 2
Add labels and legend
Show plot
End
This flow shows how matplotlib prepares and draws multiple lines step-by-step on the same plot.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y1 = [1, 4, 9, 16]
y2 = [2, 3, 5, 7]

plt.plot(x, y1, label='Squares')
plt.plot(x, y2, label='Primes')
plt.legend()
plt.show()
This code plots two lines on the same graph: squares and primes, then shows the plot with a legend.
Execution Table
StepActionData UsedPlot StateOutput
1Import matplotlib.pyplot as plt-No plot createdModule loaded
2Define x datax = [1, 2, 3, 4]No plot createdx data ready
3Define y1 datay1 = [1, 4, 9, 16]No plot createdy1 data ready
4Define y2 datay2 = [2, 3, 5, 7]No plot createdy2 data ready
5Plot line 1 with label 'Squares'x and y1One line plottedLine 1 drawn
6Plot line 2 with label 'Primes'x and y2Two lines plottedLine 2 drawn
7Add legendLabels from linesLegend addedLegend visible
8Show plotAll plotted lines and legendPlot window opensGraph displayed
9End-Plot shownExecution complete
💡 All lines plotted and displayed, program ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
xundefined[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
y1undefinedundefined[1, 4, 9, 16][1, 4, 9, 16][1, 4, 9, 16]
y2undefinedundefinedundefined[2, 3, 5, 7][2, 3, 5, 7]
plot stateemptyemptyemptyemptytwo lines with legend
Key Moments - 3 Insights
Why do we call plt.plot() twice instead of once?
Each plt.plot() call adds one line to the plot. Calling it twice draws two separate lines, as shown in steps 5 and 6 of the execution_table.
What does plt.legend() do and why is it called after plotting lines?
plt.legend() collects labels from all plotted lines and shows a legend box. It must be called after plotting lines to know their labels, as in step 7.
Why doesn't the plot show until plt.show() is called?
plt.show() opens the plot window and renders the graph. Before this, lines are prepared but not displayed, as seen in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6. How many lines are plotted after this step?
ATwo lines
BOne line
CNo lines
DThree lines
💡 Hint
Check the 'Plot State' column at step 6 in the execution_table.
According to variable_tracker, what is the value of y2 after step 4?
A[1, 4, 9, 16]
B[2, 3, 5, 7]
C[1, 2, 3, 4]
Dundefined
💡 Hint
Look at the 'y2' row and 'After Step 4' column in variable_tracker.
If we remove plt.legend(), what will happen to the plot output?
ALines will not be drawn
BPlot will not open
CPlot will show but without legend
DAn error will occur
💡 Hint
Refer to step 7 and 8 in execution_table about legend and plot display.
Concept Snapshot
Plotting multiple lines with matplotlib:
- Use plt.plot(x, y, label='name') for each line
- Call plt.legend() to show labels
- Call plt.show() to display the plot
- Each plt.plot() adds a new line
- Lines share the same axes by default
Full Transcript
This example shows how to plot multiple lines using matplotlib in Python. First, we import matplotlib.pyplot as plt. Then we prepare data lists for x and two y sets. We call plt.plot() twice, once for each line, passing x and y data with labels. After plotting lines, we call plt.legend() to add a legend box that shows line labels. Finally, plt.show() opens the plot window displaying both lines and the legend. Variables x, y1, and y2 hold the data arrays. The plot state changes from empty to having one line, then two lines, then the legend. This step-by-step process helps beginners see how multiple lines are added and displayed on the same graph.