Understand the idea
A scatter plot uses one point for each row’s pair of numeric values. The horizontal and vertical coordinates must come from the same record.
A small example
A record with hours=2 and score=70 appears at (2, 70); x chooses hours and y chooses score.
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", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()What each part does
sns.scatterplot(...)- Draw one point for each record.
data=df- Read the values from df.
x="hours"- Use hours for each point’s horizontal position.
y="score"- Use score from the same row for its vertical position.
ax=ax- Draw on the Axes created earlier in the code.
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 for hours against score.
- Chart: title "Study club"; x "hours"; y "score".
Hint
Keep x and y from the same row; one point represents one paired observation.
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", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()