Understand the idea
pairplot creates its own Figure: distributions on the diagonal and pairwise relationships off the diagonal.
A small example
For two variables, the grid shows each distribution and the relationship twice with swapped axes.
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
g = sns.pairplot(data=df, vars=["hours", "score"], hue="club", diag_kind="hist")
g.figure.tight_layout()
plt.show()What each part does
g = sns.pairplot(...)- create a whole grid and keep its handle
vars=[...]- choose numeric columns
diag_kind="hist"- histogram on the diagonal
g.figure- the grid’s Figure
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 pairplot for hours and score, hue=club, diag_kind="hist".
- This is a figure-level exception: do not create fig, ax first.
- Finish with g.figure.tight_layout() and plt.show().
Hint
pairplot owns its Figure; do not create an extra empty Figure first.
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
g = sns.pairplot(data=df, vars=["hours", "score"], hue="club", diag_kind="hist")
g.figure.tight_layout()
plt.show()