Challenge - 5 Problems
Violin Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of basic violin plot code
What will be the output of this code snippet that creates a violin plot for two groups of data?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data1 = np.random.normal(0, 1, 100) data2 = np.random.normal(5, 1, 100) plt.violinplot([data1, data2]) plt.show()
Attempts:
2 left
💡 Hint
Think about what plt.violinplot does with a list of arrays.
✗ Incorrect
plt.violinplot can take a list of datasets and draws a violin plot for each dataset side by side. Here, data1 is centered near 0 and data2 near 5, so the violins appear at those positions.
❓ data_output
intermediate1:30remaining
Number of violins plotted
Given this code, how many violins will appear in the plot?
Matplotlib
import matplotlib.pyplot as plt import numpy as np np.random.seed(0) data = [np.random.normal(loc, 1, 50) for loc in range(4)] plt.violinplot(data) plt.show()
Attempts:
2 left
💡 Hint
Count how many datasets are in the list passed to plt.violinplot.
✗ Incorrect
The list 'data' contains 4 arrays, so plt.violinplot draws 4 violins, one for each dataset.
🔧 Debug
advanced2:00remaining
Identify the error in 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(0, 1, 100) plt.violinplot(data, positions=[1, 2]) plt.show()
Attempts:
2 left
💡 Hint
Check if the number of positions matches the number of datasets.
✗ Incorrect
The data is a single array, so plt.violinplot expects one position. Providing two positions causes a ValueError due to mismatch.
❓ visualization
advanced2:00remaining
Effect of bandwidth parameter on violin plot
Which option best describes the effect of setting the 'bw_method' parameter in plt.violinplot?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.normal(0, 1, 100) plt.violinplot(data, bw_method=0.1) plt.show()
Attempts:
2 left
💡 Hint
Lower bandwidth means the kernel density estimate is more sensitive to data variations.
✗ Incorrect
Lower bw_method values make the kernel density estimate more sensitive, showing more detailed distribution shapes in the violin plot.
🧠 Conceptual
expert2:30remaining
Understanding violin plot components
Which statement correctly describes the components shown in a violin plot created by plt.violinplot?
Attempts:
2 left
💡 Hint
Think about how violin plots combine density and summary statistics.
✗ Incorrect
Violin plots show the kernel density estimate mirrored to form a shape, and can include markers for median and quartiles to summarize data.