Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Explain the evidence · V24 · 8 MIN

Multiple subplots

Manage two charts on one Figure.

Exercises within this concept

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

Understand the idea

subplots creates several plotting areas on one Figure. Send each chart to its intended Axes.

Concept sketch: one Figure, two different chart typesDistributionRelationshipone Figure, two different chart types
Illustration · not the exercise output

A small example

subplots(1, 2) creates left and right panels; subplots(2, 1) creates upper and lower panels.

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, axes = plt.subplots(1, 2, figsize=(10, 4))
sns.histplot(data=df, x="hours", bins=4, ax=axes[0])
sns.scatterplot(data=df, x="hours", y="score", ax=axes[1])
axes[0].set(title="Distribution", xlabel="hours", ylabel="Count")
axes[1].set(title="Relationship", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()

What each part does

fig, axes = plt.subplots(1, 2, figsize=(10, 4))
two plotting areas
axes[0]
first Axes
ax=axes[1]
draw on the second Axes

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 a two-panel Figure: a 4-bin histogram of hours on the left and a scatter of hours against score on the right.
  2. Use panel titles "Distribution" and "Relationship", x labels "hours", and y labels "Count" and "score".
  3. Finish the Figure with tight_layout and show.
  4. Use: subplots().
Hint

Pass each chart its own axes[0] or axes[1]; finish the whole Figure once.

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, axes = plt.subplots(1, 2, figsize=(10, 4))
sns.histplot(data=df, x="hours", bins=4, ax=axes[0])
sns.scatterplot(data=df, x="hours", y="score", ax=axes[1])
axes[0].set(title="Distribution", xlabel="hours", ylabel="Count")
axes[1].set(title="Relationship", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()
Your task · Follow
  1. Using df, create a two-panel Figure: a 4-bin histogram of hours on the left and a scatter of hours against score on the right.
  2. Use panel titles "Distribution" and "Relationship", x labels "hours", and y labels "Count" and "score".
  3. Finish the Figure with tight_layout and show.
  4. Use: subplots().

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.