Dumbbell charts in Matplotlib - Time & Space Complexity
We want to understand how the time it takes to draw a dumbbell chart changes as we add more data points.
How does the work grow when the number of points increases?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
# Data points
n = 100
x1 = range(n)
x2 = [i + 1 for i in x1]
y = range(n)
plt.hlines(y, x1, x2, color='gray')
plt.scatter(x1, y, color='blue')
plt.scatter(x2, y, color='red')
plt.show()
This code draws horizontal lines and two sets of points for each data item to create a dumbbell chart.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing lines and points for each data item.
- How many times: Once per data point, repeated n times.
Each new data point adds a line and two points to draw, so the work grows steadily with the number of points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~30 drawing actions |
| 100 | ~300 drawing actions |
| 1000 | ~3000 drawing actions |
Pattern observation: The number of drawing actions grows directly in proportion to the number of data points.
Time Complexity: O(n)
This means the time to draw the chart grows in a straight line as you add more points.
[X] Wrong: "Adding more points won't affect drawing time much because matplotlib is fast."
[OK] Correct: Each point and line must be drawn separately, so more points mean more work and longer drawing time.
Understanding how drawing time grows helps you explain performance when working with visualizations in real projects.
"What if we added a loop inside the drawing code to draw multiple lines per data point? How would the time complexity change?"
Practice
dumbbell chart in data visualization?Solution
Step 1: Understand dumbbell chart structure
Dumbbell charts show two points per category connected by a line to highlight differences or changes.Step 2: Compare with other chart types
Bar charts show single values, scatter plots show relationships, and treemaps show hierarchies, so they don't fit dumbbell chart purpose.Final Answer:
To compare two related values for each category using connected points -> Option AQuick Check:
Dumbbell chart = compare two values [OK]
- Confusing dumbbell charts with bar charts
- Thinking dumbbell charts show distributions
- Mixing up scatter plots with dumbbell charts
Solution
Step 1: Identify dumbbell chart components
Dumbbell charts use horizontal lines to connect two points and scatter points to mark values.Step 2: Match matplotlib functions
hlinesdraws horizontal lines,scatterplots points, so these two create dumbbell charts.Final Answer:
hlinesandscatter-> Option DQuick Check:
Lines + points = hlines & scatter [OK]
- Using bar instead of hlines for connecting lines
- Confusing hist with scatter
- Trying pie charts for dumbbell visualization
import matplotlib.pyplot as plt categories = ['A', 'B'] start = [1, 3] end = [4, 5] plt.hlines(y=categories, xmin=start, xmax=end, color='gray') plt.scatter(start, categories, color='blue') plt.scatter(end, categories, color='red') plt.show()
Solution
Step 1: Analyze hlines command
plt.hlines(y=categories, xmin=start, xmax=end, color='gray')fails because y-coordinates must be numeric, but categories=['A', 'B'] are strings. Raises ValueError: could not convert string to float.Step 2: Confirm scatter commands also fail
plt.scatter(start, categories)and subsequent scatter require numeric y-values, leading to the same error.Final Answer:
An error because categories cannot be used as y-values -> Option CQuick Check:
Strings as y-values in hlines/scatter -> error [OK]
- Assuming strings work directly as y-values
- Expecting horizontal dumbbell chart output
- Confusing with bar chart output
import matplotlib.pyplot as plt categories = ['X', 'Y'] start = [2, 4] end = [5, 7] plt.hlines(y=categories, xmin=start, xmax=end) plt.scatter(start, categories) plt.scatter(end, categories) plt.show()
Solution
Step 1: Check data types for plotting
hlinesandscatterboth require numeric y-values. Passing string categories as y causes ValueError (cannot convert string to float).Step 2: Fix by using numeric indices
Createy_pos = list(range(len(categories))), usey=y_posin hlines and scatters, thenplt.yticks(y_pos, categories).Final Answer:
Error because categories are strings; convert categories to numeric indices -> Option BQuick Check:
Categories (str) -> numeric indices [OK]
- Assuming hlines and scatter accept strings equally
- Ignoring axis data type requirements
- Missing that scatter needs numeric coordinates
products = ['P1', 'P2', 'P3', 'P4'], sales_2022 = [100, 150, 200, 130], sales_2023 = [120, 140, 210, 160]. How would you create a dumbbell chart to compare sales changes using matplotlib?Solution
Step 1: Understand dumbbell chart requirements
We need to show two values per product connected by a line to compare sales in 2022 and 2023.Step 2: Map data to plotting functions
hlinesdraws lines between sales_2022 and sales_2023 at y=products;scatterplots points for each year's sales.Final Answer:
Useplt.hlineswith y as products, xmin as sales_2022, xmax as sales_2023, andplt.scatterfor points -> Option AQuick Check:
Dumbbell = hlines + scatter with two values [OK]
- Using bar charts instead of dumbbell charts
- Plotting only one year's data
- Not connecting points with lines
Ready to prove it?
Pass the test and this pattern moves unknown
