0
0
Matplotlibdata~10 mins

Horizontal grouped bars 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 with the common alias.

Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Amatplotlib
Bmatplotlib.pyplot
Cpyplot
Dmpl
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'matplotlib' directly without '.pyplot' will not allow plotting.
Using an uncommon alias like 'mpl' instead of 'plt'.
2fill in blank
medium

Complete the code to create a horizontal bar chart using plt.barh.

Matplotlib
plt.[1](categories, values)
Drag options to blanks, or click blank then click option'
Abarh
Bplot
Cbar
Dhist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.bar() creates vertical bars, not horizontal.
Using plt.plot() or plt.hist() will not create bar charts.
3fill in blank
hard

Fix the error in the code to correctly position grouped bars horizontally.

Matplotlib
plt.barh(y_pos + [1], values2, height=bar_width, label='Group 2')
Drag options to blanks, or click blank then click option'
Abar_width * 2
Bbar_width / 2
C0
Dbar_width
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 shifts bars on top of the first group.
Using half or double bar width causes incorrect spacing.
4fill in blank
hard

Fill both blanks to create y positions and set bar width for grouped horizontal bars.

Matplotlib
y_pos = np.arange(len(categories))
bar_width = [1]
plt.barh(y_pos, values1, height=[2], label='Group 1')
Drag options to blanks, or click blank then click option'
A0.35
B0.5
C0.25
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using height larger than bar width causes bars to overlap.
Using 1 makes bars too thick and overlapping.
5fill in blank
hard

Fill all three blanks to complete the code for horizontal grouped bars with labels and legend.

Matplotlib
plt.barh([1], values1, height=bar_width, label='Group 1')
plt.barh([2] + [3], values2, height=bar_width, label='Group 2')
plt.yticks(y_pos + bar_width / 2, categories)
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Ay_pos
Cbar_width
Dvalues1
Attempts:
3 left
💡 Hint
Common Mistakes
Not shifting the second group causes bars to overlap.
Using values instead of y positions for bar placement.