Bubble charts concept in Matplotlib - Time & Space Complexity
We want to understand how the time to draw a bubble chart changes as we add more bubbles.
How does the number of bubbles affect the work matplotlib does?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
sizes = [100, 200, 300, 400, 500]
plt.scatter(x, y, s=sizes)
plt.show()
This code draws a bubble chart with 5 bubbles, each sized differently.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing each bubble on the chart.
- How many times: Once for each bubble in the data arrays.
As we add more bubbles, matplotlib draws more circles one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 drawing steps |
| 100 | 100 drawing steps |
| 1000 | 1000 drawing steps |
Pattern observation: The work grows directly with the number of bubbles.
Time Complexity: O(n)
This means the time to draw the chart grows in a straight line as you add more bubbles.
[X] Wrong: "Drawing many bubbles happens instantly no matter how many there are."
[OK] Correct: Each bubble needs to be drawn separately, so more bubbles mean more work and more time.
Knowing how drawing time grows helps you understand performance when visualizing large data sets.
"What if we added animation to the bubbles? How would that affect the time complexity?"
Practice
matplotlib?Solution
Step 1: Understand bubble chart data dimensions
A bubble chart shows data points with x and y coordinates plus a size dimension.Step 2: Identify what bubble size represents
The size of each bubble encodes a third data value, like quantity or magnitude.Final Answer:
A third data value representing magnitude or quantity -> Option BQuick Check:
Bubble size = third data value [OK]
- Confusing size with color
- Thinking size is x or y position
- Ignoring the third data dimension
matplotlib function parameter controls the bubble sizes in a bubble chart?Solution
Step 1: Recall
Theplt.scatterparametersplt.scatterfunction usessto set bubble sizes.Step 2: Confirm correct parameter name
Other options likesizeorradiusare not valid parameters for bubble size inplt.scatter.Final Answer:
s -> Option DQuick Check:
plt.scatter(x, y, s=...)sets bubble size [OK]
- Using 'size' instead of 's'
- Trying 'radius' which is invalid
- Confusing with color parameter
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [100, 200, 300] plt.scatter(x, y, s=sizes) plt.show()
Solution
Step 1: Analyze the code parameters
The code usesplt.scatterwith x, y coordinates ands=sizeswhere sizes are [100, 200, 300].Step 2: Understand bubble size mapping
Each bubble size corresponds to the values in the sizes list, so bubbles will have sizes 100, 200, and 300.Final Answer:
A scatter plot with three bubbles sized 100, 200, and 300 respectively -> Option CQuick Check:
Bubble sizes match 's' list values [OK]
- Assuming all bubbles have same size
- Confusing sizes with x or y values
- Expecting syntax error incorrectly
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [100, 200] plt.scatter(x, y, s=sizes) plt.show()
Solution
Step 1: Check lengths of x, y, and sizes
x and y have 3 elements each, but sizes has only 2 elements.Step 2: Understand size array length requirement
The sizes array must match the number of points to assign size to each bubble.Final Answer:
The sizes list length does not match x and y lengths -> Option AQuick Check:
Sizes length must equal x and y length [OK]
- Ignoring size list length mismatch
- Thinking 's' parameter is misspelled
- Believing x and y must be strings
Solution
Step 1: Understand impact of raw sizes
Using raw sales values with zeros and large numbers causes bubbles too small or too large, making chart unreadable.Step 2: Normalize and scale sizes
Normalizing sales data (e.g., min-max scaling) and applying a size factor keeps bubbles proportional and visible.Step 3: Evaluate other options
Fixed sizes lose data meaning; using sales as x-axis changes chart type and loses size info.Final Answer:
Normalize sales data and apply a size scaling factor -> Option AQuick Check:
Normalize and scale sizes for readability [OK]
- Using raw sizes causing unreadable bubbles
- Fixing all sizes losing data info
- Misusing sales data as axis instead of size
Ready to prove it?
Pass the test and this pattern moves unknown
