Understand the idea
Use a small number of visual mappings so a scatter plot stays readable. Mapping the same group to colour and shape gives two ways to recognize it.
A small example
Club A might be blue circles and club B orange crosses; the legend explains both cues.
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.scatterplot(
data=df, x="hours", y="score", hue="club", style="club", size="hours", ax=ax
)
ax.set(title="Study club", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()What each part does
hue=- category colours
style=- category marker shapes
size=- numeric marker areas
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.scatterplot with x=hours, y=score, hue=club, style=club and size=hours.
- Chart: title "Study club"; x "hours"; y "score".
Hint
Use the same category for hue and style so colour is not the only cue.
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.scatterplot(
data=df, x="hours", y="score", hue="club", style="club", size="hours", ax=ax
)
ax.set(title="Study club", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()