0
0
Matplotlibdata~20 mins

Statistical plot enhancements in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Statistical Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Effect of adding a KDE curve to a histogram
What will be the output of this code snippet that plots a histogram with a KDE curve using matplotlib and seaborn?
Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

data = np.random.normal(loc=0, scale=1, size=1000)

sns.histplot(data, kde=True, color='skyblue')
plt.title('Histogram with KDE curve')
plt.show()
AA histogram with blue bars only, no KDE curve
BA KDE curve only, no histogram bars
CA histogram with blue bars and a smooth KDE curve overlayed in black
DA scatter plot of the data points
Attempts:
2 left
💡 Hint
The 'kde=True' parameter adds a smooth curve over the histogram.
data_output
intermediate
1:30remaining
Number of bins in a histogram with automatic binning
Given this code, how many bins will the histogram have approximately?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.random.uniform(0, 10, 500)
plt.hist(data, bins='auto')
plt.show()
AAbout 50 bins
BAbout 20 bins
CAbout 30 bins
DAbout 10 bins
Attempts:
2 left
💡 Hint
The 'auto' binning method tries to find a good number of bins based on data size and distribution.
visualization
advanced
2:30remaining
Customizing boxplot whiskers to show 5th and 95th percentiles
Which code snippet correctly creates a boxplot where whiskers extend to the 5th and 95th percentiles instead of the default 1.5 IQR?
Aplt.boxplot(data, whis=(5, 95))
Bplt.boxplot(data, whis=[0.05, 0.95])
Cplt.boxplot(data, whis=(0.05, 0.95))
Dplt.boxplot(data, whis=[5, 95])
Attempts:
2 left
💡 Hint
The 'whis' parameter accepts percentiles (0-100) when given as a tuple.
🔧 Debug
advanced
2:00remaining
Identify the error in this violin plot code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(size=100), np.random.normal(loc=2, size=100)]
plt.violinplot(data, positions=[1, 2, 3])
plt.show()
AValueError: positions length does not match data length
BTypeError: data must be a 1D array
CNo error, plot shows correctly
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check if the number of positions matches the number of datasets.
🚀 Application
expert
3:00remaining
Enhancing a scatter plot with marginal histograms
Which code snippet correctly creates a scatter plot with histograms on the top and right margins using matplotlib's gridspec?
AUse plt.subplots with gridspec_kw={'width_ratios':[1,4],'height_ratios':[1,4]} and plot scatter on top-left, histograms on bottom-right and top-right
BUse plt.subplots with gridspec_kw={'width_ratios':[1,4],'height_ratios':[4,1]} and plot scatter on top-right, histograms on bottom-left and top-left
CUse plt.subplots with gridspec_kw={'width_ratios':[4,1],'height_ratios':[4,1]} and plot scatter on bottom-right, histograms on top-right and bottom-left
DUse plt.subplots with gridspec_kw={'width_ratios':[4,1],'height_ratios':[1,4]} and plot scatter on bottom-left, histograms on top-left and bottom-right
Attempts:
2 left
💡 Hint
The scatter plot should occupy the larger bottom-left area, histograms on top and right margins.