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 · V33 · 8 MIN

Faceting

Repeat the same chart across comparable subsets.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeCompare category distributions across panels
  3. TransferChoose distribution panels for a new context

Understand the idea

Faceting repeats a chart for different subsets of one category. These Seaborn functions create the Figure and panels for you.

Concept sketch: same chart and scales, separate subsetsgroup Agroup Bsame chart and scales, separate subsets
Illustration · not the exercise output

A small example

col="station" creates one panel per station; each panel uses only records from that station.

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

g = sns.relplot(data=df, x="hours", y="score", col="club", kind="scatter", height=3)
g.figure.tight_layout()
plt.show()

What each part does

g = sns.relplot(...)
Create a whole grid of relationship charts and store it as g.
x="hours", y="score"
Plot those two measurements within each panel.
col="club"
Make one panel for each club, using only its rows.
kind="scatter"
Draw points rather than lines.
height=3
Make each panel 3 inches high.
g.figure.tight_layout()
Arrange the complete grid before display.

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, create relplot scatter panels of hours against score, split by club, using height=3.
  2. This is a figure-level exception: do not create fig, ax first.
  3. Finish with g.figure.tight_layout() and plt.show().
Hint

Figure-level functions create their own panels; height describes each panel.

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

g = sns.relplot(data=df, x="hours", y="score", col="club", kind="scatter", height=3)
g.figure.tight_layout()
plt.show()
Your task · Follow
  1. Using df, create relplot scatter panels of hours against score, split by club, using height=3.
  2. This is a figure-level exception: do not create fig, ax first.
  3. Finish with g.figure.tight_layout() and plt.show().

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.