0
0
Matplotlibdata~10 mins

Grouped 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 import the matplotlib library.

Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Amatplotlib.pyplot
Bnumpy
Cpandas
Dseaborn
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong library like numpy or pandas.
Forgetting to import pyplot specifically.
2fill in blank
medium

Complete the code to create a list of categories for the x-axis.

Matplotlib
categories = ['A', 'B', 'C', [1]]
Drag options to blanks, or click blank then click option'
A4
B'1'
C['D']
D'D'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number instead of a string for category.
Adding a list inside the list.
3fill in blank
hard

Fix the error in the code to set the width of each bar.

Matplotlib
bar_width = [1]
Drag options to blanks, or click blank then click option'
A1.5
B0.5
C5
D'0.5'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number.
Using a width larger than 1 causing bars to overlap.
4fill in blank
hard

Fill both blanks to correctly calculate the x positions for the second group of bars.

Matplotlib
x2 = [x + [1] for x in x1]
plt.bar(x2, group2, width=[2], label='Group 2')
Drag options to blanks, or click blank then click option'
Abar_width
B0.1
Cbar_width / 2
Dbar_width * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a smaller shift causing bars to overlap.
Using different widths causing uneven bars.
5fill in blank
hard

Fill all three blanks to create a grouped bar chart with labels and legend.

Matplotlib
plt.bar(x1, [1], width=[2], label='Group 1')
plt.bar(x2, [3], width=bar_width, label='Group 2')
plt.legend()
Drag options to blanks, or click blank then click option'
Agroup1
Bbar_width
Cgroup2
Dcategories
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up data variables for groups.
Forgetting to add legend for clarity.