Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the matplotlib plotting library.
Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'matplotlib' directly instead of 'matplotlib.pyplot'.
Using 'import pyplot as plt' without 'matplotlib'.
✗ Incorrect
We import the pyplot module from matplotlib as plt to create plots.
2fill in blank
mediumComplete the code to create two lists of data for histograms.
Matplotlib
data1 = [1, 2, 3, 4, 5] data2 = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same list for both data sets.
Using an empty list.
✗ Incorrect
We create a second list of numbers to overlay a second histogram.
3fill in blank
hardFix the error in the code to plot the first histogram with 5 bins.
Matplotlib
plt.hist(data1, bins=[1])
plt.show() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing bins as a string like '5'.
Passing bins as a list like [5].
✗ Incorrect
The bins parameter expects an integer, not a string or list.
4fill in blank
hardFill both blanks to plot two histograms overlayed with transparency.
Matplotlib
plt.hist(data1, bins=[1], alpha=[2]) plt.hist(data2, bins=5, alpha=0.5) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different bins for the two histograms.
Using alpha=1 which hides the histogram underneath.
✗ Incorrect
We use 5 bins and alpha=0.5 for transparency to see both histograms clearly.
5fill in blank
hardFill all three blanks to add labels and a legend to the overlayed histograms.
Matplotlib
plt.hist(data1, bins=5, alpha=0.5, label=[1]) plt.hist(data2, bins=5, alpha=0.5, label=[2]) plt.[3]() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not adding labels to histograms.
Using plt.show() instead of plt.legend() to show legend.
✗ Incorrect
Labels name each histogram, and plt.legend() shows the legend on the plot.