Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to plot a cumulative histogram using matplotlib.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.randn(1000) plt.hist(data, bins=30, cumulative=[1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cumulative=False will plot a normal histogram.
Passing None or 0 will not create a cumulative histogram.
✗ Incorrect
Setting cumulative=True makes the histogram cumulative.
2fill in blank
mediumComplete the code to add labels and a title to the cumulative histogram.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.randn(1000) plt.hist(data, bins=20, cumulative=True) plt.xlabel([1]) plt.ylabel('Frequency') plt.title('Cumulative Histogram') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Bins' as xlabel is less clear than 'Values'.
Leaving xlabel empty makes the plot less understandable.
✗ Incorrect
The x-axis label should describe the data values shown in the histogram.
3fill in blank
hardFix the error in the code to correctly plot a cumulative histogram with density normalization.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.randn(1000) plt.hist(data, bins=25, cumulative=[1], density=True) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting cumulative=False will plot a normal histogram.
Using None or 0 for cumulative causes the histogram to be non-cumulative.
✗ Incorrect
To plot a cumulative histogram with density normalization, set cumulative=True.
4fill in blank
hardFill both blanks to create a cumulative histogram with 40 bins and a blue color.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.randn(1000) plt.hist(data, bins=[1], cumulative=True, color=[2]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string for bins causes an error.
Using a color name without quotes causes an error.
✗ Incorrect
Use 40 bins and set color to 'blue' for the histogram.
5fill in blank
hardFill all three blanks to create a cumulative histogram with green color, 50 bins, and alpha transparency 0.5.
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.randn(1000) plt.hist(data, bins=[1], cumulative=True, color=[2], alpha=[3]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using alpha as a string causes an error.
Using color without quotes causes an error.
✗ Incorrect
Set bins to 50, color to 'green', and alpha to 0.5 for transparency.