Plotting multiple lines in Matplotlib - Time & Space Complexity
When plotting multiple lines with matplotlib, it's important to understand how the time to draw grows as we add more lines.
We want to know how the plotting time changes when we increase the number of lines.
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
for i in range(50):
y = np.sin(x + i)
plt.plot(x, y)
plt.show()
This code plots 50 sine waves, each shifted by a different amount, on the same graph.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that calls
plt.plot()50 times to draw each line. - How many times: Once for each line, so 50 times in this example.
Each additional line adds roughly the same amount of work to draw.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 plot calls |
| 100 | 100 plot calls |
| 1000 | 1000 plot calls |
Pattern observation: The work grows linearly as the number of lines increases.
Time Complexity: O(n)
This means the time to plot grows directly in proportion to the number of lines you draw.
[X] Wrong: "Plotting multiple lines is almost as fast as plotting one line because the data points are the same."
[OK] Correct: Each line requires a separate draw call and rendering, so the time adds up with each new line.
Understanding how plotting time grows helps you write efficient visualization code and explain performance in real projects.
"What if we plotted all lines using a single call with multiple y arrays? How would the time complexity change?"
Practice
matplotlib?Solution
Step 1: Understand plotting multiple lines
In matplotlib, to plot multiple lines on the same graph, you callplt.plot()for each line's data before showing the plot.Step 2: Identify correct plotting function
plt.scatter(),plt.bar(), andplt.hist()are for scatter plots, bar charts, and histograms, not line plots.Final Answer:
Callplt.plot()multiple times with different data sets beforeplt.show()-> Option CQuick Check:
Multipleplt.plot()calls = multiple lines [OK]
- Using plt.scatter() instead of plt.plot() for lines
- Calling plt.show() after each line instead of once
- Using bar or histogram functions for line plots
Solution
Step 1: Check order of plotting and showing
To plot multiple lines, callplt.plot()for each line, then callplt.show()once at the end.Step 2: Identify correct snippet
plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) plt.show() callsplt.plot()twice thenplt.show(), which is correct. plt.plot([1, 2, 3], [4, 5, 6]) plt.show() plt.plot([1, 2, 3], [6, 5, 4]) callsplt.show()too early, splitting the plots. plt.show() plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) callsplt.show()before plotting, so nothing shows. plt.plot([1, 2, 3], [4, 5, 6]) plt.plot([1, 2, 3], [6, 5, 4]) missesplt.show()entirely.Final Answer:
Two plt.plot() calls followed by plt.show() -> Option AQuick Check:
Plot lines first, show once after [OK]
- Calling plt.show() before all plots
- Forgetting plt.show() to display plot
- Plotting after plt.show() call
import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [2, 4, 6] y2 = [1, 3, 5] plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') plt.legend() plt.show()
Solution
Step 1: Analyze plotting commands
The code plots two lines withplt.plot()and assigns labels 'Line 1' and 'Line 2'.Step 2: Understand legend usage
Callingplt.legend()displays the legend with the labels for both lines.Final Answer:
A graph with two lines labeled 'Line 1' and 'Line 2' -> Option BQuick Check:
Labels + plt.legend() = labeled lines [OK]
- Forgetting plt.legend() so labels don't show
- Assigning labels incorrectly
- Plotting only one line
import matplotlib.pyplot as plt x = [1, 2, 3] y1 = [2, 4, 6] y2 = [1, 3, 5] plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') plt.legend plt.show()
Solution
Step 1: Check function calls
The code callsplt.legendwithout parentheses, so the legend function is not executed.Step 2: Understand effect of missing parentheses
Without callingplt.legend(), the legend does not appear, so labels won't show.Final Answer:
Missing parentheses after plt.legend() call -> Option AQuick Check:
Call plt.legend() with () to show legend [OK]
- Forgetting () after plt.legend
- Assuming legend shows without calling function
- Mixing up order of plt.show()
Solution
Step 1: Understand color and style shorthand
In matplotlib, shorthand like 'r-' means red solid line, 'g--' green dashed, 'b:' blue dotted.Step 2: Check label usage and legend
plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--', label='Green dashed') plt.plot(x, y3, 'b:', label='Blue dotted') plt.legend() plt.show() assigns labels to all lines and callsplt.legend()to show them. plt.plot(x, y1, color='red', linestyle='solid', label='Red solid') plt.plot(x, y2, color='green', linestyle='dashed', label='Green dashed') plt.plot(x, y3, color='blue', linestyle='dotted', label='Blue dotted') plt.legend() plt.show() uses invalid linestyle names ('solid', 'dashed', 'dotted' are not valid; correct are '-', '--', ':'). plt.plot(x, y1, 'red', 'solid', label='Red solid') plt.plot(x, y2, 'green', 'dashed', label='Green dashed') plt.plot(x, y3, 'blue', 'dotted', label='Blue dotted') plt.legend() plt.show() uses positional strings incorrectly. plt.plot(x, y1, 'r-', label='Red solid') plt.plot(x, y2, 'g--') plt.plot(x, y3, 'b:') plt.legend() plt.show() misses labels for second and third lines.Final Answer:
Shorthand format strings like 'r-', 'g--', 'b:' for each plt.plot() -> Option DQuick Check:
Use shorthand 'color-style' and label each line [OK]
- Using invalid linestyle names like 'solid'
- Forgetting labels on all lines
- Not calling plt.legend() to show labels
Ready to prove it?
Pass the test and this pattern moves unknown
