Understand the idea
A kernel density estimate (KDE) smooths observations into a curve. Its height is density, not a count or a probability at one exact value.
A small example
Increasing bw_adjust from 1 to 2 makes bumps wider and the combined curve smoother; it does not add observations.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(6, 4))
sns.kdeplot(data=df, x="hours", bw_adjust=1, cut=0, ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Density")
fig.tight_layout()
plt.show()What each part does
sns.kdeplot- a smoothed density estimate
bw_adjust=1- default bandwidth multiplier
cut=0- stay within observed endpoints
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| student | club | hours | score | group |
|---|---|---|---|---|
| Ari | Art | 1 | 52 | A |
| Bo | Code | 3 | 71 | A |
| Cy | Art | 2 | 65 | B |
| Dee | Code | 4 | 82 | B |
| Eli | Art | 5 | 89 | A |
| Flo | Code | 3 | 76 | A |
| Gus | Art | 6 | 93 | B |
| Han | Code | 2 | 61 | B |
Your task · Follow
- Using df, call sns.kdeplot for hours, with bw_adjust=1 and cut=0.
- Chart: title "Study club"; x "hours"; y "Density".
Hint
Density is not a count. cut=0 prevents the curve extending beyond observed values.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(6, 4))
sns.kdeplot(data=df, x="hours", bw_adjust=1, cut=0, ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Density")
fig.tight_layout()
plt.show()