Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Relational and Categorical Plots

Line Plots with lineplot

Use seaborn lineplot to show trends: aggregation of repeated measurements, confidence intervals, hue and style groups, markers, sorting and relplot facets.

A line plot connects points in order, usually along time or another ordered axis. sns.lineplot does more than draw lines: if you have several observations at the same x, it aggregates them, drawing the mean as a line and a confidence interval as a shaded band. That makes it as much a statistics tool as a drawing tool.

A first line plot

Python Mean line with a confidence band

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

There are 25 rows for each week, but seaborn draws a single line. For every x value it computed the mean and a 95% confidence interval (by default estimated with bootstrapping), and drew the interval as a band. A narrow band means the mean is well estimated; a wide band means it is uncertain.

Change what is shown with:

Argument Effect
estimator="median" A different aggregation ("sum", "max", a function, or None for raw lines)
errorbar=("ci", 95) Default: 95% confidence interval
errorbar="sd" Band shows one standard deviation (spread of the data)
errorbar=("pi", 50) Band shows the middle 50% of the data
errorbar=None No band
err_style="bars" Error bars instead of a band

The difference between "ci" (uncertainty of the mean) and "sd" or "pi" (spread of the data) is important and has its own lesson in Error Bars and Confidence Intervals.

Groups with hue, style and size

Just like scatterplot, lineplot takes hue, style and size. Each group gets its own line (and band).

Python Lines for two groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The left plot shows that the means differ and the bands barely overlap later on, so the plans are likely different. The right plot uses standard deviation bands, which are wider because they show how much individual customers vary rather than how well the mean is known.

Sorting and one line per entity

By default lineplot sorts the data by x before drawing, so lines always run left to right. If your data is a path where order matters (a GPS track, a hysteresis curve), pass sort=False.

If you have one row per x for each of many entities (customers, sensors) and want one thin line per entity instead of an aggregate, use units="customer_id" with estimator=None:

Python
sns.lineplot(data=readings, x="hour", y="temp", units="sensor", estimator=None,
             color="gray", alpha=0.3, linewidth=0.8)

This draws each sensor's raw series, a good way to see individual behavior before averaging.

Dates on the x axis

If x is a datetime column, seaborn formats dates automatically. Long date labels may overlap: rotate them with plt.xticks(rotation=30) or use fig.autofmt_xdate(). Time series techniques such as resampling and rolling means are in Time Series Plots and Rolling Averages.

Wide-form data

As described in Tidy Data, sns.lineplot(data=wide_df) draws one line per column, with the index on x. It is quick for a handful of series, but long-form data lets you use hue, style and col=.

relplot(kind="line")

For small multiples, use sns.relplot(kind="line", col="region"). It also puts the legend outside the grid.

Python
g = sns.relplot(data=usage, x="week", y="minutes", hue="plan", col="region",
                kind="line", height=3, aspect=1.2)

Common mistakes

  • Joining points that should not be connected (categories on x). Lines imply order and continuity; use bars or points otherwise.
  • Not noticing that a band appeared: if you are plotting one value per x, no band is drawn; if several, it is.
  • Reading a confidence band as the data range. It is the uncertainty of the average.
  • Plotting thousands of unaggregated points with a bootstrap band: it can be slow; use errorbar=None or errorbar="sd".
  • Forgetting sort=False for paths.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You have 25 rows for each week and call lineplot(x='week', y='minutes'). What is drawn?

    lineplot aggregates repeated x values: mean plus a bootstrapped confidence interval.
  2. 2. Which setting shows the spread of the data rather than the uncertainty of the mean?

    'sd' (standard deviation) and 'pi' (percentile interval) describe the data spread.
  3. 3. How do you draw one raw line per sensor without aggregation?

    units groups rows into separate lines and estimator=None disables the aggregation.
  4. 4. A GPS track must be drawn in recorded order. Which argument?

    By default lineplot sorts by x; sort=False keeps the row order.

Summary

  • lineplot connects points in x order and aggregates repeated x values with a mean and a 95% confidence band.
  • Change the statistic with estimator and the band with errorbar ("ci", "sd", "pi", None).
  • Use hue, style and size for groups; add markers=True for readability.
  • units with estimator=None draws individual series; sort=False keeps row order.
  • relplot(kind="line") adds faceting.

Next: Categorical Plots: Choosing the Right One