Understand the idea
A chart needs a title that says what is being shown and axis labels that identify the measurements and units.
A small example
x="temperature" selects data; xlabel="Temperature (°C)" changes the displayed label without renaming the data column.
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
ax.set(title=..., xlabel=..., ylabel=...)- name the chart and axes
ax.set_title(...)- change just the title
fig.tight_layout()- adjust spacing
plt.show()- 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
- Create a scatter plot from df, x=hours, y=score, at 6 by 4 inches.
- Give the chart title "Study club" and axis labels "hours" and "score".
- Finish with fig.tight_layout() and plt.show().
- Use: set().
Hint
Axis labels describe the mapped measurements, while the title describes the question.
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()