Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Make a useful report · V37 · 20 MIN

Visualise checkpoint

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
Task 1 · Study club

Using df, keep rows with hours greater than 3 in selected. Use these same records for all three charts.

The editable setup on the right creates df. Run executes the setup and your work from top to bottom.

Your requirements

  1. Figure 1: four-bin histogram of hours. Title Distribution; axis labels hours and Count.
  2. Figure 2: scatter of hours against score. Title Relationship; axis labels hours and score.
  3. Figure 3: exact bars of total hours by club. Title Totals; axis labels club and Total.
  4. Create three separate Figures. Finish each with tight_layout() and display each with plt.show().

Your inputs

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

Revisit the concept lesson →

Remember the idea

Concept sketch: filter first, then compare three viewsPairsSpreadSummaryfilter first, then compare three views

Build several views of the same selected population so the charts answer a coherent question.

A small example

A distribution and a grouped mean can complement each other only when their populations are clear.

What each choice does
Code or choiceMeaning
Select onceStore the eligible records and reuse them for every Figure.
Choose each summaryA histogram counts observations; a scatter pairs values; exact bars use calculated means or totals.
Finish each FigureSet its labels, arrange the layout and display it.
Hint

Define selected once, then reuse it for all three Figures. Compute the grouped summary before drawing exact bars.

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

selected = df[df["hours"] > 3]

fig, ax = plt.subplots(figsize=(6, 4))
sns.histplot(data=selected, x="hours", bins=4, ax=ax)
ax.set(title="Distribution", xlabel="hours", ylabel="Count")
fig.tight_layout()
plt.show()

fig, ax = plt.subplots(figsize=(6, 4))
sns.scatterplot(data=selected, x="hours", y="score", ax=ax)
ax.set(title="Relationship", xlabel="hours", ylabel="score")
fig.tight_layout()
plt.show()

totals = selected.groupby("club")["hours"].sum()
fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(totals.index, totals.values)
ax.set(title="Totals", xlabel="club", ylabel="Total")
fig.tight_layout()
plt.show()
Your task · Task 1
  1. Using df, keep rows with hours greater than 3 in selected. Use these same records for all three charts.
  2. Figure 1: four-bin histogram of hours. Title Distribution; axis labels hours and Count.
  3. Figure 2: scatter of hours against score. Title Relationship; axis labels hours and score.
  4. Figure 3: exact bars of total hours by club. Title Totals; axis labels club and Total.
  5. Create three separate Figures. Finish each with tight_layout() and display each with 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.