3D surface plots in Matplotlib - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When creating 3D surface plots, the time it takes depends on how many points we draw. We want to understand how the drawing time grows as we add more points.
How does the number of points affect the time to create the plot?
Analyze the time complexity of the following code snippet.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
n = 50 # Example value for n
x = np.linspace(-5, 5, n)
y = np.linspace(-5, 5, n)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()
This code creates a 3D surface plot using a grid of points sized by n. It calculates values and draws the surface.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating values for each point on an
nbyngrid and drawing them. - How many times: The calculation and plotting happen for every one of the
n * npoints.
As n grows, the number of points grows by the square of n. So the work grows quickly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 (10 x 10) |
| 100 | 10,000 (100 x 100) |
| 1000 | 1,000,000 (1000 x 1000) |
Pattern observation: Doubling n makes the work about four times bigger because we have a grid of points.
Time Complexity: O(n2)
This means the time to create the 3D surface plot grows roughly with the square of the grid size.
[X] Wrong: "Increasing the grid size n only makes the plot take a little longer, like just doubling the time."
[OK] Correct: Because the grid is two-dimensional, increasing n means many more points (n times n), so the time grows much faster than just doubling.
Understanding how plotting time grows with data size helps you explain performance in data visualization tasks. This skill shows you can think about efficiency even when working with graphics.
"What if we used a 1D line plot instead of a 3D surface plot? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of 3D surface plots
3D surface plots visualize how two inputs relate to an output by showing a curved surface in three dimensions.Step 2: Compare with other plot types
Unlike 2D line graphs or bar charts, 3D surface plots show a continuous surface representing output values over a grid of inputs.Final Answer:
The relationship between two input variables and one output variable as a curved surface -> Option AQuick Check:
3D surface plot = curved surface of inputs and output [OK]
- Confusing 3D surface plots with 2D line plots
- Thinking it shows only one variable distribution
- Mixing up bar charts with surface plots
Solution
Step 1: Recall the standard import for 3D plotting
Matplotlib uses mpl_toolkits.mplot3d to enable 3D plotting, and the correct import is from mpl_toolkits.mplot3d import Axes3D.Step 2: Check other options for correctness
Options A, C, and D are not valid matplotlib import statements for 3D plotting.Final Answer:
from mpl_toolkits.mplot3d import Axes3D -> Option CQuick Check:
3D import = mpl_toolkits.mplot3d Axes3D [OK]
- Trying to import non-existent modules
- Using wrong aliases like plt3d
- Assuming 3D is included by default in pyplot
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.linspace(-5, 5, 10) y = np.linspace(-5, 5, 10) X, Y = np.meshgrid(x, y) Z = X**2 + Y**2 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z) plt.show()
Solution
Step 1: Analyze the function Z = X^2 + Y^2
This function creates a paraboloid shape, which looks like a bowl opening upwards.Step 2: Understand the plot_surface call
plot_surface plots the Z values over the grid defined by X and Y, producing a smooth 3D surface.Final Answer:
A 3D surface plot showing a bowl-shaped paraboloid -> Option AQuick Check:
plot_surface with X^2+Y^2 = bowl shape [OK]
- Confusing surface plot with scatter plot
- Expecting 2D plot instead of 3D
- Missing meshgrid usage for X, Y
import numpy as np import matplotlib.pyplot as plt x = np.linspace(-3, 3, 50) y = np.linspace(-3, 3, 50) X, Y = np.meshgrid(x, y) Z = np.sin(np.sqrt(X**2 + Y**2)) fig = plt.figure() ax = fig.add_subplot(111) ax.plot_surface(X, Y, Z) plt.show()
Solution
Step 1: Check subplot creation for 3D plotting
To plot 3D surfaces, the subplot must have projection='3d'. The code misses this, so ax is 2D.Step 2: Verify other parts
Z calculation and meshgrid usage are correct. plt.show() is present.Final Answer:
Missing projection='3d' in add_subplot -> Option BQuick Check:
3D plot needs projection='3d' [OK]
- Forgetting projection='3d' in add_subplot
- Misusing meshgrid or Z calculation
- Omitting plt.show()
Z = sin(X) * cos(Y) over the range -π to π for both X and Y with a smooth surface and a color map that highlights height differences. Which of the following code snippets correctly achieves this?Solution
Step 1: Check function and range correctness
The correct code uses Z = np.sin(X) * np.cos(Y) over -np.pi to np.pi with 100 points for smoothness.Step 2: Verify 3D plotting and color map usage
The correct code uses projection='3d', plot_surface with cmap='viridis', and adds a colorbar to highlight height differences.Step 3: Identify errors in other options
import numpy as np import matplotlib.pyplot as plt x = np.linspace(-np.pi, np.pi, 100) y = np.linspace(-np.pi, np.pi, 100) X, Y = np.meshgrid(x, y) Z = np.sin(X) * np.cos(Y) plt.plot_surface(X, Y, Z, cmap='plasma') plt.show() misses 3D axis creation; import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.linspace(-np.pi, np.pi, 50) y = np.linspace(-np.pi, np.pi, 50) X, Y = np.meshgrid(x, y) Z = np.sin(X) + np.cos(Y) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z) plt.show() uses wrong function Z and fewer points; import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.linspace(-np.pi, np.pi, 100) y = np.linspace(-np.pi, np.pi, 100) X, Y = np.meshgrid(x, y) Z = np.sin(X) * np.cos(Y) fig = plt.figure() ax = fig.add_subplot(111) ax.plot_surface(X, Y, Z, cmap='coolwarm') plt.show() misses projection='3d' in subplot.Final Answer:
The code with projection='3d', cmap='viridis', colorbar, correct Z, and 100 points -> Option DQuick Check:
projection='3d' + cmap='viridis' + colorbar + Z=sin(X)*cos(Y) + 100 pts [OK]
- Forgetting projection='3d' in subplot
- Using wrong function for Z
- Not adding color map or colorbar
- Calling plot_surface without axis object
