0
0
Matplotlibdata~20 mins

Dumbbell charts in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dumbbell Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dumbbell Chart Code
What will be the output of this Python code that creates a dumbbell chart using matplotlib?
Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values_1 = [10, 20, 30]
values_2 = [15, 18, 35]

plt.hlines(y=categories, xmin=values_1, xmax=values_2, color='gray')
plt.scatter(values_1, categories, color='blue', label='Start')
plt.scatter(values_2, categories, color='red', label='End')
plt.legend()
plt.show()
AA scatter plot with points only for values_1 and values_2 without connecting lines
BA vertical bar chart with three bars for categories A, B, and C
CA horizontal dumbbell chart with three lines connecting blue and red dots for categories A, B, and C
DA line plot connecting values_1 and values_2 points in sequence
Attempts:
2 left
💡 Hint
Look at how hlines and scatter are used to connect points horizontally.
data_output
intermediate
1:30remaining
Number of Lines in Dumbbell Chart
Given the following data, how many horizontal lines will be drawn in the dumbbell chart code below?
Matplotlib
categories = ['X', 'Y', 'Z', 'W']
values_1 = [5, 10, 15, 20]
values_2 = [7, 12, 18, 25]

# Lines drawn using plt.hlines(y=categories, xmin=values_1, xmax=values_2)
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Each category corresponds to one horizontal line.
visualization
advanced
2:00remaining
Identify the Dumbbell Chart Visualization
Which option best describes the visual output of this dumbbell chart code snippet?
Matplotlib
import matplotlib.pyplot as plt

labels = ['Group1', 'Group2']
start = [100, 150]
end = [120, 130]

plt.hlines(y=labels, xmin=start, xmax=end, color='green', linewidth=4)
plt.scatter(start, labels, color='orange', s=100)
plt.scatter(end, labels, color='purple', s=100)
plt.show()
ATwo horizontal thick green lines with orange dots on the left and purple dots on the right for each group
BA scatter plot with orange and purple dots randomly placed
CTwo vertical green lines with orange and purple dots stacked vertically
DA bar chart with green bars and dots on top
Attempts:
2 left
💡 Hint
Check the use of hlines and scatter with horizontal y-axis labels.
🔧 Debug
advanced
2:00remaining
Error in Dumbbell Chart Code
What error will this dumbbell chart code produce when run?
Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values_1 = [10, 20]
values_2 = [15, 25, 30]

plt.hlines(y=categories, xmin=values_1, xmax=values_2, color='black')
plt.scatter(values_1, categories, color='blue')
plt.scatter(values_2, categories, color='red')
plt.show()
ANo error, chart displays correctly
BTypeError because categories are strings
CIndexError from accessing out-of-range list elements
DValueError due to mismatched lengths of y and xmin arrays
Attempts:
2 left
💡 Hint
Check if all input lists have the same length.
🚀 Application
expert
2:30remaining
Calculate Difference from Dumbbell Chart Data
Given the following data used in a dumbbell chart, what is the average difference between the two value sets?
Matplotlib
categories = ['P', 'Q', 'R', 'S']
values_1 = [50, 60, 70, 80]
values_2 = [55, 65, 75, 90]

differences = [v2 - v1 for v1, v2 in zip(values_1, values_2)]
avg_diff = sum(differences) / len(differences)
print(round(avg_diff, 2))
A10.0
B6.25
C5.0
D7.5
Attempts:
2 left
💡 Hint
Calculate each difference, then average them.