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

Stacked bars

Build a part-to-whole bar from known components.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt a requirement
  3. TransferCombine earlier skills

Understand the idea

Stacked bars add components with the same units. The bottom parameter sets where the upper segment starts.

Concept sketch: each total is the sum of both components0ABCpart 1part 2each total is the sum of both components
Illustration · not the exercise output

A small example

A base count of 3 and upper count of 2 make a total height of 5; the upper segment starts at 3.

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))
first = df["Solo"]
second = df["Two players"]
ax.bar(df["genre"], first, label="Solo")
ax.bar(df["genre"], second, bottom=first, label="Two players")
ax.legend()
ax.set(title="Game sessions", xlabel="genre", ylabel="Count")
fig.tight_layout()
plt.show()

What each part does

ax.bar(..., bottom=first)
stack above first
label=
name each component
ax.legend()
show component names

Your inputs

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

Game sessions · 4 synthetic rows
genreSoloTwo players
Puzzle21
Strategy12
Adventure02
Family31

Your task · Follow

  1. Use df's actual Solo and Two players counts to draw stacked bars by genre, in table order.
  2. Put Solo at the base and Two players above it; label both components in a legend.
  3. Chart: title "Game sessions"; x "genre"; y "Count".
Hint

The upper bars need bottom equal to the lower component, not zero.

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))
first = df["Solo"]
second = df["Two players"]
ax.bar(df["genre"], first, label="Solo")
ax.bar(df["genre"], second, bottom=first, label="Two players")
ax.legend()
ax.set(title="Game sessions", xlabel="genre", ylabel="Count")
fig.tight_layout()
plt.show()
Your task · Follow
  1. Use df's actual Solo and Two players counts to draw stacked bars by genre, in table order.
  2. Put Solo at the base and Two players above it; label both components in a legend.
  3. Chart: title "Game sessions"; x "genre"; y "Count".

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.