Challenge - 5 Problems
Dumbbell Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Look at how hlines and scatter are used to connect points horizontally.
✗ Incorrect
The code uses horizontal lines (hlines) to connect two sets of values for each category, then plots scatter points at the start and end values, creating a dumbbell chart.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
Each category corresponds to one horizontal line.
✗ Incorrect
There are 4 categories, so 4 horizontal lines will be drawn connecting the start and end values for each category.
❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
Check the use of hlines and scatter with horizontal y-axis labels.
✗ Incorrect
The code draws horizontal thick green lines between start and end values for each group, with colored dots marking the ends, forming a dumbbell chart.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check if all input lists have the same length.
✗ Incorrect
The y-axis categories list has length 3, but values_1 has length 2, causing a ValueError when plotting hlines.
🚀 Application
expert2: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))
Attempts:
2 left
💡 Hint
Calculate each difference, then average them.
✗ Incorrect
Differences are [5, 5, 5, 10]. Their average is (5+5+5+10)/4 = 6.25. But careful: sum is 25, average is 6.25. The code prints 6.25, so option B is correct.