Using df, keep rows with hours greater than 3 in selected. Use these same records for all three charts.
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
Your requirements
- Figure 1: four-bin histogram of hours. Title Distribution; axis labels hours and Count.
- Figure 2: scatter of hours against score. Title Relationship; axis labels hours and score.
- Figure 3: exact bars of total hours by club. Title Totals; axis labels club and Total.
- Create three separate Figures. Finish each with tight_layout() and display each with plt.show().
Your inputs
| 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 |
Remember the idea
Build several views of the same selected population so the charts answer a coherent question.
A small example
A distribution and a grouped mean can complement each other only when their populations are clear.
| Code or choice | Meaning |
|---|---|
Select once | Store the eligible records and reuse them for every Figure. |
Choose each summary | A histogram counts observations; a scatter pairs values; exact bars use calculated means or totals. |
Finish each Figure | Set its labels, arrange the layout and display it. |
Hint
Define selected once, then reuse it for all three Figures. Compute the grouped summary before drawing exact bars.
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
selected = df[df["hours"] > 3]
fig, ax = plt.subplots(figsize=(6, 4))
sns.histplot(data=selected, x="hours", bins=4, ax=ax)
ax.set(title="Distribution", xlabel="hours", ylabel="Count")
fig.tight_layout()
plt.show()
fig, ax = plt.subplots(figsize=(6, 4))
sns.scatterplot(data=selected, x="hours", y="score", ax=ax)
ax.set(title="Relationship", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()
totals = selected.groupby("club")["hours"].sum()
fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(totals.index, totals.values)
ax.set(title="Totals", xlabel="club", ylabel="Total")
fig.tight_layout()
plt.show()