0
0
Matplotlibdata~10 mins

Dumbbell charts in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dumbbell charts
Prepare data with two values per category
Plot points for first values
Plot points for second values
Draw lines connecting paired points
Customize labels, colors, and axes
Show chart
Dumbbell charts show two values per category connected by a line, highlighting differences visually.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
val1 = [10, 20, 30]
val2 = [15, 18, 35]
plt.scatter(val1, categories)
plt.scatter(val2, categories)
plt.hlines(categories, val1, val2)
plt.show()
This code plots two sets of points per category and connects them with horizontal lines to form a dumbbell chart.
Execution Table
StepActionData/VariablesPlot Elements CreatedResult
1Import matplotlib.pyplot as pltplt module loadedNoneReady to plot
2Define categoriescategories = ['A', 'B', 'C']NoneCategories set
3Define first valuesval1 = [10, 20, 30]NoneFirst values set
4Define second valuesval2 = [15, 18, 35]NoneSecond values set
5Plot first pointsplt.scatter(val1, categories)Scatter points at (10,A), (20,B), (30,C)First points visible
6Plot second pointsplt.scatter(val2, categories)Scatter points at (15,A), (18,B), (35,C)Second points visible
7Draw horizontal linesplt.hlines(categories, val1, val2)Lines connecting (10,A)-(15,A), (20,B)-(18,B), (30,C)-(35,C)Lines visible connecting points
8Show plotplt.show()Full dumbbell chart displayedChart visible on screen
💡 Plot displayed and execution ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
categoriesundefined['A', 'B', 'C']['A', 'B', 'C']['A', 'B', 'C']['A', 'B', 'C']
val1undefinedundefined[10, 20, 30][10, 20, 30][10, 20, 30]
val2undefinedundefinedundefined[15, 18, 35][15, 18, 35]
Key Moments - 3 Insights
Why do we use plt.hlines with categories as y-values?
Because categories are on the y-axis, plt.hlines draws horizontal lines at each category level connecting the two x-values (val1 and val2). See execution_table step 7.
What happens if val1 and val2 have different lengths?
Matplotlib will raise an error because scatter and hlines require matching lengths for x and y data. All arrays must align as shown in execution_table steps 5-7.
Why do we plot scatter points twice?
Each scatter plots one set of values (val1 and val2) so we see both points per category. This is shown in execution_table steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what does plt.hlines do?
ADraws vertical lines connecting categories
BDraws horizontal lines connecting val1 and val2 at each category
CPlots scatter points for val1
DShows the plot window
💡 Hint
Check the 'Plot Elements Created' column at step 7 in execution_table
According to variable_tracker, what is the value of val2 after step 4?
A[10, 20, 30]
Bundefined
C[15, 18, 35]
D['A', 'B', 'C']
💡 Hint
Look at the val2 row and After Step 4 column in variable_tracker
If val1 was [10, 20] instead of length 3, what would happen?
AMatplotlib would raise an error due to length mismatch
BThe third category would be plotted with zero value
CThe plot would show only two categories
DThe plot would ignore extra val2 values
💡 Hint
Refer to key_moments about array length matching and execution_table steps 5-7
Concept Snapshot
Dumbbell charts show two values per category connected by lines.
Use plt.scatter() twice for points, plt.hlines() to connect.
Categories on y-axis, values on x-axis.
Ensure val1, val2, and categories have same length.
Visualize differences clearly between paired values.
Full Transcript
Dumbbell charts help compare two values per category by plotting points and connecting them with lines. First, we prepare data arrays for categories and two sets of values. Then, we plot scatter points for each value set at the categories on the y-axis. Next, horizontal lines connect each pair of points to highlight differences. Finally, we show the plot. Variables categories, val1, and val2 hold the data. Scatter plots create points, and hlines draw connecting lines. Matching array lengths are important to avoid errors. This visual method clearly shows differences between paired data points.