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 · models and matrices · V19 · 8 MIN

Understand lineplot aggregation

Notice when a line summarises repeated x values.

Exercises within this concept

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

Understand the idea

When several rows share an x value, a line can summarize them or follow one repeated measurement series.

Concept sketch: line = mean; band = ±1 SD at each xline = mean; band = ±1 SD at each x
Illustration · not the exercise output

A small example

At day 1, replicates A=4 and B=8 have mean 6. Selecting replicate A instead plots its observed value 4.

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.lineplot(
    data=df, x="day", y="sales", estimator="mean", errorbar="sd", marker="o", ax=ax
)
ax.set(title="Daily sales · paired observations", xlabel="day", ylabel="sales")
fig.tight_layout()
plt.show()

What each part does

estimator="mean"
one mean for each x
errorbar="sd"
spread, not a confidence interval
Other choices for later exercises
estimator=None
retain raw values

Your inputs

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

Daily sales · paired observations · 8 synthetic rows
daysalesreplicate
112A
114B
216A
218B
314A
316B
420A
422B

Your task · Follow

  1. Using df's paired measurements, plot mean sales at each day, with one-standard-deviation error bands and "o" markers.
  2. Chart: title "Daily sales · paired observations"; x "day"; y "sales".
  3. Use: lineplot().
Hint

Repeated times are aggregated when estimator is mean; the SD band shows within-time spread.

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.lineplot(
    data=df, x="day", y="sales", estimator="mean", errorbar="sd", marker="o", ax=ax
)
ax.set(title="Daily sales · paired observations", xlabel="day", ylabel="sales")
fig.tight_layout()
plt.show()
Optional stretch

Use estimator=None and errorbar=None. Why do repeated x values now have several y values?

Your task · Follow
  1. Using df's paired measurements, plot mean sales at each day, with one-standard-deviation error bands and "o" markers.
  2. Chart: title "Daily sales · paired observations"; x "day"; y "sales".
  3. Use: lineplot().

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.