Heatmap with plt.imshow in Matplotlib - Time & Space Complexity
We want to understand how the time to create a heatmap grows as the data size increases.
How does the plotting time change when the matrix gets bigger?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
# Create a random matrix of size n x n
n = 100
data = np.random.rand(n, n)
# Plot heatmap
plt.imshow(data, cmap='viridis')
plt.colorbar()
plt.show()
This code creates a heatmap from a square matrix of size n by n using plt.imshow.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Rendering each cell of the n x n matrix as a colored pixel.
- How many times: Once for each of the n² cells in the matrix.
As the matrix size n grows, the number of cells to draw grows with n squared.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: Doubling n makes the work about four times bigger because the area grows with n squared.
Time Complexity: O(n²)
This means the time to draw the heatmap grows roughly with the square of the matrix size.
[X] Wrong: "The time grows linearly with n because we just loop once over the data."
[OK] Correct: The data is two-dimensional, so we actually process n times n cells, not just n.
Understanding how plotting time grows with data size helps you explain performance in data visualization tasks clearly and confidently.
"What if we changed the data from a square matrix to a long vector? How would the time complexity change?"
Practice
plt.imshow function do in matplotlib when given a 2D array?Solution
Step 1: Understand plt.imshow input
The function takes a 2D array and maps its values to colors in a grid.Step 2: Recognize heatmap behavior
This color mapping creates a heatmap, showing patterns visually.Final Answer:
It creates a heatmap by showing the array values as colors. -> Option CQuick Check:
plt.imshow = heatmap colors [OK]
- Thinking plt.imshow plots lines or text
- Confusing heatmap with pie chart
- Assuming plt.imshow prints numbers
plt.imshow?Solution
Step 1: Recall color bar function
Matplotlib usesplt.colorbar()to add a color scale next to images.Step 2: Check other options
Other options are not valid matplotlib functions.Final Answer:
plt.colorbar() -> Option AQuick Check:
Color bar function = plt.colorbar() [OK]
- Using non-existent functions like plt.addcolor()
- Forgetting to call plt.colorbar() after plt.imshow()
- Trying to add color bar before plotting heatmap
import matplotlib.pyplot as plt import numpy as np data = np.array([[1, 2], [3, 4]]) plt.imshow(data, cmap='hot') plt.colorbar() plt.show()
Solution
Step 1: Understand plt.imshow with cmap='hot'
The data array is 2x2, so plt.imshow creates a 2x2 grid colored by values using the 'hot' color map (black to red to yellow).Step 2: Effect of plt.colorbar()
The color bar shows the scale from the minimum value 1 to maximum 4, helping interpret colors.Final Answer:
A 2x2 heatmap with colors from black to red to yellow, and a color bar showing values 1 to 4. -> Option DQuick Check:
plt.imshow + cmap='hot' + colorbar = heatmap with hot colors [OK]
- Thinking plt.imshow plots lines or scatter
- Believing cmap='hot' causes error
- Ignoring color bar meaning
import matplotlib.pyplot as plt import numpy as np data = np.random.rand(3,3) plt.imshow(data) plt.colorbar plt.show()
Solution
Step 1: Identify plt.colorbar usage
The code usesplt.colorbarwithout parentheses, so the function is not called.Step 2: Correct function call
Adding parenthesesplt.colorbar()calls the function and displays the color bar.Final Answer:
plt.colorbar is missing parentheses, so color bar is not shown. -> Option AQuick Check:
Call plt.colorbar() with () to show color bar [OK]
- Forgetting parentheses on plt.colorbar
- Thinking cmap is required for color bar
- Calling plt.show() before plt.colorbar
plt.imshow that shows the values clearly with a color bar, but you want the color scale to always be from 0 to 20 regardless of data. Which code snippet correctly does this?Solution
Step 1: Set color scale limits in plt.imshow
Usevmin=0andvmax=20inplt.imshowto fix color scale from 0 to 20.Step 2: Add color bar normally
Callplt.colorbar()to show the color scale matching the fixed limits.Final Answer:
plt.imshow(matrix, vmin=0, vmax=20) plt.colorbar() plt.show() -> Option BQuick Check:
Set vmin/vmax in imshow, then add colorbar [OK]
- Setting vmin/vmax in plt.colorbar instead of plt.imshow
- Not setting vmax to 20, so scale auto adjusts
- Using wrong vmin/vmax values
Ready to prove it?
Pass the test and this pattern moves unknown
