Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Compare observations · V17 · 8 MIN

Add dimensions to scatter

Use colour and shape without losing readability.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt a requirement
  3. TransferChoose and combine

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.

Concept sketch: group = shape/colour; magnitude = sizegroup = shape/colour; magnitude = size
Illustration · not the exercise output

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.

Study club · 8 synthetic rows
studentclubhoursscoregroup
AriArt152A
BoCode371A
CyArt265B
DeeCode482B
EliArt589A
FloCode376A
GusArt693B
HanCode261B

Your task · Follow

  1. Using df, call sns.scatterplot with x=hours, y=score, hue=club, style=club and size=hours.
  2. 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()
Your task · Follow
  1. Using df, call sns.scatterplot with x=hours, y=score, hue=club, style=club and size=hours.
  2. Chart: title "Study club"; x "hours"; y "score".

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.