Marker size variation in Matplotlib - Time & Space Complexity
When we change marker sizes in a plot, the computer must draw each marker with its own size. We want to understand how the time to draw grows as we add more markers.
How does the drawing time increase when we vary marker sizes for many points?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1000)
y = np.random.rand(1000)
sizes = np.linspace(10, 200, 1000)
plt.scatter(x, y, s=sizes)
plt.show()
This code plots 1000 points with marker sizes changing from small to large.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing each marker with its specific size.
- How many times: Once for each point, here 1000 times.
Each new point adds one more marker to draw, so the total work grows as we add points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 marker draws |
| 100 | 100 marker draws |
| 1000 | 1000 marker draws |
Pattern observation: The drawing time grows directly with the number of points.
Time Complexity: O(n)
This means the time to draw markers grows in a straight line as we add more points.
[X] Wrong: "Changing marker sizes makes the drawing time grow faster than the number of points."
[OK] Correct: Each marker is drawn once regardless of size; size changes do not multiply the number of drawing steps.
Understanding how drawing time grows with data size helps you explain performance in data visualization tasks clearly and confidently.
What if we added a loop to redraw each marker multiple times? How would the time complexity change?
Practice
s parameter control in the plt.scatter() function in matplotlib?Solution
Step 1: Understand the role of
Theplt.scatter()plt.scatter()function creates scatter plots where each point can have different visual properties.Step 2: Identify the
Thesparameter purposesparameter specifically sets the size of each marker in the scatter plot, either as a single value or a list of sizes.Final Answer:
The size of the markers in the scatter plot -> Option AQuick Check:
scontrols marker size [OK]
- Confusing 's' with color or shape parameters
- Thinking 's' controls transparency
- Assuming 's' changes marker style
Solution
Step 1: Recall the parameter name for marker size
The correct parameter to set marker size inplt.scatter()iss.Step 2: Check the syntax for setting size
Usings=50sets all markers to size 50. Other options use incorrect parameter names.Final Answer:
plt.scatter(x, y, s=50) -> Option BQuick Check:
Usesfor size [OK]
s to set size, not 'size' or 'sizes' [OK]- Using 'size' or 'sizes' instead of 's'
- Trying to set marker size with 'marker_size'
- Syntax errors from wrong parameter names
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [20, 50, 80] plt.scatter(x, y, s=sizes) plt.show()
Solution
Step 1: Understand
Theswith list inputsparameter accepts a list to assign different sizes to each marker.Step 2: Match sizes to points
Sincesizes = [20, 50, 80], the first point gets size 20, second 50, third 80.Final Answer:
Markers will have sizes 20, 50, and 80 respectively -> Option DQuick Check:
List inssets sizes per point [OK]
s sets sizes point-wise [OK]- Assuming all markers get the first size
- Thinking list input causes error
- Confusing
swith color list
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [30, 60] plt.scatter(x, y, s=sizes) plt.show()
Solution
Step 1: Check lengths of data and sizes
The x and y lists have 3 points, but sizes list has only 2 values.Step 2: Understand size list requirement
Theslist must match the number of points; otherwise, matplotlib raises a ValueError.Final Answer:
The sizes list length does not match x and y length -> Option CQuick Check:
Sizes list length must equal data points [OK]
- Ignoring size list length mismatch
- Thinking
scan't be a list - Assuming matplotlib auto-fills missing sizes
cities = ['A', 'B', 'C'] population = [1000, 5000, 3000] x = [1, 2, 3] y = [4, 5, 6]
Which code correctly scales the population to marker sizes and plots them?
Solution
Step 1: Understand marker size scaling
Population values are large; directly using them as sizes will create huge markers.Step 2: Choose appropriate scaling
Multiplying population by 10 scales sizes visibly but keeps them reasonable. Dividing by 10 makes markers too small; squaring makes them too large.Final Answer:
plt.scatter(x, y, s=[p*10 for p in population]) -> Option AQuick Check:
Scale sizes for visibility using multiplication [OK]
- Using raw large values causing huge markers
- Dividing sizes too much making markers tiny
- Using squared sizes causing disproportionate markers
Ready to prove it?
Pass the test and this pattern moves unknown
