Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Optional · figures and composition · VR6 · 12 MIN

Review · figures and composition

Task 1 · Retrieve and combine

Exercises within this concept

  1. Task 1Retrieve and combineCurrent exercise
  2. Task 2Retrieve and combine
  3. Task 3Retrieve and combine
  4. Task 4Retrieve and combine
Task 1 · Board games

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

  1. Annotate the highest-rated selected game as Peak, offset (8, 8) points with an arrow.
  2. Chart: title "Board games"; x "minutes"; y "rating".

Your inputs

Board games · 6 synthetic rows
gamegenreminutesratingplayers
OrbitStrategy204.1Two
TilesPuzzle153.8Solo
QuestAdventure604.7Two
GroveStrategy354.4Solo
CastleAdventure454.5Two
CardsPuzzle103.6Solo

Revisit the concept lesson →

Remember the idea

Concept sketch: arrow identifies the highest observationPeakarrow identifies the highest observation

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.

What each choice does
Code or choiceMeaning
idxmax() / idxmin()Return the row label of the largest / smallest value (first if tied). Use loc to retrieve that row.
xy / xytextData 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()
Your task · Task 1
  1. Using df games lasting at least 20 minutes, plot minutes against rating.
  2. Annotate the highest-rated selected game as Peak, offset (8, 8) points with an arrow.
  3. Chart: title "Board games"; x "minutes"; y "rating".

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Display charts with plt.show().

Python starts when you open a lesson.

Output

Run your code to see what Python returns.