Challenge - 5 Problems
Stacked Bar Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a basic stacked bar chart code
What will be the output of this code snippet that creates a stacked bar chart using matplotlib?
Matplotlib
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] values1 = [3, 5, 1] values2 = [2, 3, 4] plt.bar(labels, values1, label='Group 1') plt.bar(labels, values2, bottom=values1, label='Group 2') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Stacked bars use the 'bottom' parameter to stack one bar on top of another.
✗ Incorrect
The code uses plt.bar twice with the same labels. The second bar uses 'bottom=values1' to stack on top of the first bars, creating a stacked bar chart.
❓ data_output
intermediate1:30remaining
Resulting heights of stacked bars
Given these data arrays for two groups, what is the total height of the stacked bars for each label after stacking?
Matplotlib
values1 = [4, 2, 7] values2 = [1, 3, 2] # Total height is values1 + values2 element-wise
Attempts:
2 left
💡 Hint
Add the values from both groups for each label.
✗ Incorrect
The total height of each stacked bar is the sum of the corresponding values from values1 and values2.
🔧 Debug
advanced2:00remaining
Identify the error in this stacked bar chart code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt labels = ['X', 'Y'] values1 = [3, 4] values2 = [2, 5, 1] plt.bar(labels, values1, label='First') plt.bar(labels, values2, bottom=values1, label='Second') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Check if the lengths of values arrays match the labels.
✗ Incorrect
values2 has 3 elements but labels has 2, so matplotlib raises a ValueError about mismatched lengths.
❓ visualization
advanced1:30remaining
Interpreting a stacked bar chart visualization
You see a stacked bar chart with three bars labeled Jan, Feb, Mar. Each bar has two colors: blue on bottom and orange on top. The blue segments are 5, 3, 4 units tall respectively. The orange segments are 2, 6, 1 units tall respectively. What is the total height of the bar for February?
Attempts:
2 left
💡 Hint
Add the heights of the stacked segments for February.
✗ Incorrect
February's total height is blue segment (3) plus orange segment (6) = 9 units.
🚀 Application
expert2:30remaining
Calculate percentage contribution in stacked bars
Given these data for three categories over two groups, calculate the percentage contribution of Group 2 to the total stacked bar height for category 'B'.
Category labels: ['A', 'B', 'C']
Group 1 values: [10, 20, 30]
Group 2 values: [5, 15, 10]
What is the percentage contribution of Group 2 in category 'B'?
Attempts:
2 left
💡 Hint
Percentage = (Group 2 value / (Group 1 value + Group 2 value)) * 100
✗ Incorrect
For category B, Group 1 is 20 and Group 2 is 15. Total is 35. Percentage = (15/35)*100 = 42.86%.