Using df games lasting at least 20 minutes, plot minutes against rating.
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
Your requirements
- Annotate the highest-rated selected game as Peak, offset (8, 8) points with an arrow.
- Chart: title "Board games"; x "minutes"; y "rating".
Your inputs
| game | genre | minutes | rating | players |
|---|---|---|---|---|
| Orbit | Strategy | 20 | 4.1 | Two |
| Tiles | Puzzle | 15 | 3.8 | Solo |
| Quest | Adventure | 60 | 4.7 | Two |
| Grove | Strategy | 35 | 4.4 | Solo |
| Castle | Adventure | 45 | 4.5 | Two |
| Cards | Puzzle | 10 | 3.6 | Solo |
Remember the idea
An annotation links text to a specific observation. Select the relevant row first so both coordinates describe the same point.
A small example
xy=(2, 70) points to that data location; xytext=(8, 8) with offset points places text a little right and above it.
| Code or choice | Meaning |
|---|---|
idxmax() / idxmin() | Return the row label of the largest / smallest value (first if tied). Use loc to retrieve that row. |
xy / xytext | Data location being labelled / text location. |
textcoords="offset points" | Interpret xytext as a typographic-point offset from xy. |
arrowprops={"arrowstyle": "->"} | Draw an arrow from the text to the observation. |
Hint
Find the peak inside the report population, not in the original table.
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))
selected = df[df["minutes"] >= 20]
sns.scatterplot(data=selected, x="minutes", y="rating", ax=ax)
peak = selected.loc[selected["rating"].idxmax()]
ax.annotate(
"Peak",
xy=(peak["minutes"], peak["rating"]),
xytext=(8, 8),
textcoords="offset points",
arrowprops={"arrowstyle": "->"},
)
ax.set(title="Board games", xlabel="minutes", ylabel="rating")
fig.tight_layout()
plt.show()