NumPy with Matplotlib - Time & Space Complexity
We want to understand how the time needed to run NumPy and Matplotlib code changes as the data size grows.
Specifically, how does plotting data from NumPy arrays affect execution time?
Analyze the time complexity of the following code snippet.
import numpy as np
import matplotlib.pyplot as plt
n = 1000
x = np.linspace(0, 10, n)
y = np.sin(x)
plt.plot(x, y)
plt.show()
This code creates an array of n points, computes their sine values, and plots them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Computing sine for each of the n points in the array.
- How many times: Once for each of the n elements.
As n grows, the number of sine calculations and points to plot grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 sine calculations and 10 plot points |
| 100 | About 100 sine calculations and 100 plot points |
| 1000 | About 1000 sine calculations and 1000 plot points |
Pattern observation: Doubling n roughly doubles the work done.
Time Complexity: O(n)
This means the time to compute and plot grows directly with the number of points.
[X] Wrong: "Plotting always takes constant time regardless of data size."
[OK] Correct: Plotting needs to process each data point, so more points mean more work and longer time.
Understanding how data size affects plotting time helps you explain performance in data visualization tasks clearly and confidently.
"What if we used a scatter plot instead of a line plot? How would the time complexity change?"
Practice
Solution
Step 1: Understand NumPy's role
NumPy helps create and manage numerical data easily.Step 2: Understand Matplotlib's role
Matplotlib is used to draw graphs to visualize data.Final Answer:
To create numerical data and then visualize it with graphs -> Option AQuick Check:
NumPy + Matplotlib = Data + Graphs [OK]
- Confusing Matplotlib with text editing tools
- Thinking NumPy is for website design
- Mixing file storage with data visualization
Solution
Step 1: Check NumPy import syntax
Correct import is 'import numpy as np' to use 'np' as shortcut.Step 2: Check Matplotlib import syntax
Correct import is 'import matplotlib.pyplot as plt' to use 'plt' for plotting.Final Answer:
import numpy as np\nimport matplotlib.pyplot as plt -> Option DQuick Check:
Standard imports = import numpy as np import matplotlib.pyplot as plt [OK]
- Using wrong aliases like 'import matplotlib as plt'
- Forgetting 'pyplot' in Matplotlib import
- Trying to import non-existing modules
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.show()
Solution
Step 1: Understand data creation
np.linspace creates 100 points from 0 to 10; y is sine of x values.Step 2: Understand plotting command
plt.plot draws a line graph connecting (x, y) points; plt.show displays it.Final Answer:
A line graph showing a sine wave -> Option AQuick Check:
plot(x, sin(x)) = sine wave line [OK]
- Confusing plt.plot with bar or scatter plots
- Expecting random points instead of line
- Missing plt.show() to display plot
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 50) y = np.cos(x) plt.plot(x, y) plt.show
Solution
Step 1: Check plt.show usage
plt.show is a function and must be called with parentheses: plt.show()Step 2: Verify other parts
np.linspace and np.cos are correct; x and y lengths match.Final Answer:
Missing parentheses after plt.show to display the plot -> Option CQuick Check:
plt.show() needed to display plot [OK]
- Forgetting parentheses on plt.show
- Thinking np.cos is invalid
- Assuming array length mismatch without checking
Solution
Step 1: Check plotting both functions
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) plt.legend(['sin', 'cos']) plt.show() plots sine and cosine separately with x values, correct usage.Step 2: Check legend and show
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) plt.legend(['sin', 'cos']) plt.show() adds legend for clarity and calls plt.show() properly.Final Answer:
Code that plots sine and cosine with legend and shows plot -> Option BQuick Check:
Separate plt.plot calls + legend + plt.show() = import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) plt.legend(['sin', 'cos']) plt.show() [OK]
- Plotting without x values causing wrong x-axis
- Missing legend to identify lines
- Using scatter instead of line plot for smooth curves
Ready to prove it?
Pass the test and this pattern moves unknown
