Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a stacked bar chart using matplotlib.
Matplotlib
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] values1 = [3, 5, 1] values2 = [4, 2, 6] plt.bar(labels, values1, label='Group 1') plt.bar(labels, values2, bottom=[1], label='Group 2') plt.legend() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values2' as bottom causes bars to overlap incorrectly.
Using 'labels' as bottom causes a type error.
✗ Incorrect
The 'bottom' parameter in plt.bar specifies where the second bar starts. To stack bars, it should be the height of the first bar, which is 'values1'.
2fill in blank
mediumComplete the code to add colors to the stacked bars.
Matplotlib
import matplotlib.pyplot as plt labels = ['X', 'Y', 'Z'] values1 = [2, 7, 4] values2 = [5, 3, 6] plt.bar(labels, values1, color=[1], label='First') plt.bar(labels, values2, bottom=values1, color='orange', label='Second') plt.legend() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using color without quotes causes a NameError.
Using an invalid color name causes a ValueError.
✗ Incorrect
The 'color' parameter accepts a color name as a string. Here, 'red' is used for the first bars.
3fill in blank
hardFix the error in the code to correctly stack three bars.
Matplotlib
import matplotlib.pyplot as plt labels = ['P', 'Q', 'R'] values1 = [1, 4, 3] values2 = [2, 5, 1] values3 = [3, 2, 4] plt.bar(labels, values1, label='One') plt.bar(labels, values2, bottom=[1], label='Two') plt.bar(labels, values3, bottom=[a+b for a,b in zip(values1, values2)], label='Three') plt.legend() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values2' as bottom for the second bar causes overlap.
Using 'labels' as bottom causes a type error.
✗ Incorrect
The second bar's bottom should be the height of the first bars, which is 'values1'. The third bar's bottom is the sum of the first and second bars, 'values1 + values2'.
4fill in blank
hardFill both blanks to create a stacked bar chart with labels and a title.
Matplotlib
import matplotlib.pyplot as plt labels = ['Jan', 'Feb', 'Mar'] sales1 = [10, 15, 7] sales2 = [5, 8, 12] plt.bar(labels, sales1, color='blue', label='Online') plt.bar(labels, sales2, bottom=[1], color='green', label=[2]) plt.xlabel('Month') plt.title('Monthly Sales') plt.legend() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sales2' as bottom causes bars to overlap incorrectly.
Using 'Online' as label for the second bar duplicates the first bar's label.
✗ Incorrect
The second bar's bottom is the first bar's heights 'sales1'. The label for the second bar is 'Offline'.
5fill in blank
hardFill all three blanks to create a stacked bar chart with three groups and custom colors.
Matplotlib
import matplotlib.pyplot as plt labels = ['East', 'West', 'North'] group1 = [4, 6, 5] group2 = [3, 7, 2] group3 = [5, 2, 4] plt.bar(labels, group1, color=[1], label='Group 1') plt.bar(labels, group2, bottom=[2], color=[3], label='Group 2') plt.bar(labels, group3, bottom=[a+b for a,b in zip(group1, group2)], color='purple', label='Group 3') plt.legend() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'group2' as bottom for the second bar causes overlap.
Using colors without quotes causes errors.
✗ Incorrect
The first bar uses color 'cyan'. The second bar's bottom is the first group's heights 'group1'. The second bar's color is 'magenta'.