0
0
Matplotlibdata~10 mins

Stacked bar charts in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Avalues1
Bvalues2
Clabels
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values2' as bottom causes bars to overlap incorrectly.
Using 'labels' as bottom causes a type error.
2fill in blank
medium

Complete 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'
A'yellow'
B'red'
C'blue'
D'green'
Attempts:
3 left
💡 Hint
Common Mistakes
Using color without quotes causes a NameError.
Using an invalid color name causes a ValueError.
3fill in blank
hard

Fix 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'
Avalues1
Bvalues2
Cvalues1 + values2
Dlabels
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values2' as bottom for the second bar causes overlap.
Using 'labels' as bottom causes a type error.
4fill in blank
hard

Fill 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'
Asales1
B'Offline'
C'Online'
Dsales2
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.
5fill in blank
hard

Fill 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'
A'cyan'
Bgroup1
C'magenta'
Dgroup2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'group2' as bottom for the second bar causes overlap.
Using colors without quotes causes errors.