0
0
Matplotlibdata~10 mins

Cumulative histograms 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 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'
ATrue
BFalse
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using cumulative=False will plot a normal histogram.
Passing None or 0 will not create a cumulative histogram.
2fill in blank
medium

Complete 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'
A'Values'
B'Bins'
C'Data values'
D'X-axis'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Bins' as xlabel is less clear than 'Values'.
Leaving xlabel empty makes the plot less understandable.
3fill in blank
hard

Fix 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'
A0
BTrue
CNone
DFalse
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.
4fill in blank
hard

Fill 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'
A40
B'red'
C'blue'
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string for bins causes an error.
Using a color name without quotes causes an error.
5fill in blank
hard

Fill 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'
A40
B50
C'green'
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using alpha as a string causes an error.
Using color without quotes causes an error.