Understand the idea
Faceting repeats a chart for different subsets of one category. These Seaborn functions create the Figure and panels for you.
A small example
col="station" creates one panel per station; each panel uses only records from that station.
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.relplot(data=df, x="hours", y="score", col="club", kind="scatter", height=3)
g.figure.tight_layout()
plt.show()What each part does
g = sns.relplot(...)- Create a whole grid of relationship charts and store it as g.
x="hours", y="score"- Plot those two measurements within each panel.
col="club"- Make one panel for each club, using only its rows.
kind="scatter"- Draw points rather than lines.
height=3- Make each panel 3 inches high.
g.figure.tight_layout()- Arrange the complete grid before display.
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, create relplot scatter panels of hours against score, split by club, using height=3.
- This is a figure-level exception: do not create fig, ax first.
- Finish with g.figure.tight_layout() and plt.show().
Hint
Figure-level functions create their own panels; height describes each panel.
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.relplot(data=df, x="hours", y="score", col="club", kind="scatter", height=3)
g.figure.tight_layout()
plt.show()