Understand the idea
A histogram divides the numeric axis into intervals and counts observations inside each one.
A small example
With values [1, 2, 3, 7] and edges [0, 4, 8], the bars have heights 3 and 1.
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.histplot(data=df, x="hours", bins=4, ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Count")
fig.tight_layout()
plt.show()What each part does
sns.histplot- count values inside bins
bins=4- four intervals
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.histplot for hours with 4 bins.
- Chart: title "Study club"; x "hours"; y "Count".
Hint
Bins group a numeric measurement; the vertical height here is a count.
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.histplot(data=df, x="hours", bins=4, ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Count")
fig.tight_layout()
plt.show()