Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Compare observations · V18 · 8 MIN

Line plot

Connect observations along a meaningful time sequence.

Exercises within this concept

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

Understand the idea

A line connects measurements along an ordered axis such as time. Decide whether repeated times represent raw observations or a summary.

Concept sketch: connect observations in time order1234Dayconnect observations in time order
Illustration · not the exercise output

A small example

At times 1, 2, 3 with sales 4, 7, 5, the line follows those values in time order.

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=None, marker="o", ax=ax)
ax.set(title="Daily sales", xlabel="day", ylabel="sales")
fig.tight_layout()
plt.show()

What each part does

sns.lineplot
connect observations along x
estimator=None
do not average repeated x
marker="o"
show the observed points

Your inputs

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

Daily sales · 5 synthetic rows
daysales
112
215
313
418
521

Your task · Follow

  1. Plot df's sales over day as a line with observation markers ("o") and no aggregation.
  2. Chart: title "Daily sales"; x "day"; y "sales".
  3. Use: lineplot().
Hint

A line implies order; estimator=None preserves observations instead of averaging repeated times.

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=None, marker="o", ax=ax)
ax.set(title="Daily sales", xlabel="day", ylabel="sales")
fig.tight_layout()
plt.show()
Your task · Follow
  1. Plot df's sales over day as a line with observation markers ("o") and no aggregation.
  2. Chart: title "Daily sales"; 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.