Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'matplotlib' directly without '.pyplot' will not allow plotting.
Using an uncommon alias like 'mpl' instead of 'plt'.
✗ Incorrect
We import the pyplot module from matplotlib as plt to create plots.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The function barh creates horizontal bar charts.
3fill in blank
hardFix 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'
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.
✗ Incorrect
Adding bar_width shifts the second group bars to the right for grouping.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using height larger than bar width causes bars to overlap.
Using 1 makes bars too thick and overlapping.
✗ Incorrect
Setting bar_width and height to 0.35 controls bar thickness and spacing.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not shifting the second group causes bars to overlap.
Using values instead of y positions for bar placement.
✗ Incorrect
The first bar group is at y_pos, the second is shifted by bar_width for grouping.