liveBook · Manning

Line plot

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A chart that represents observations as points connected by line segments, typically to show change across an ordered dimension.

A line plot uses Seaborn to show how two numeric columns relate by drawing line segments between plotted values. It is useful when the x-axis has an ordered meaning, such as days, dates, or temperatures. The `relplot` function selects this form with `kind='line'`. Categorical variables supplied through `hue` can produce separate colored lines, while `row` can separate crowded categories into individual plots. section 11.16 section 11.18

When creating a line plot, work through the data shape before interpreting the picture:

  • Check that the DataFrame index is unique; reset it with `df.reset_index(drop=True)` when duplicate index values prevent Seaborn from plotting. section 11.16
  • Choose x and y columns that answer the analytical question, such as day versus average trip distance or date versus minimum temperature. section 11.17 section 11.8
  • Use `hue` to compare categories on shared axes, and use `row` when multiple lines make the combined chart difficult to read. section 11.16
  • Decide whether confidence intervals help; pass `ci=None` when the display should show the lines without the surrounding intervals. section 11.18

Compare taxi distance by day and month

After extracting `year`, `month`, and `day` from pickup timestamps and retaining January and July 2020, plot daily trip distance with one line per month: `sns.relplot(x='day', y='trip_distance', kind='line', data=df, hue='month', ci=None)`. To plot daily trip counts instead, group by `month` and `day`, count a column, reset the index, and use the resulting count column as y. The same pattern extends to weather data: put dates on x, minimum temperatures on y, and cities in `hue`. section 11.18 section 11.8

sns.relplot(x='max_temp',
            y='min_temp',
            data=df,
            hue='state', kind='line')

Inspect the compact Seaborn call that turns a relationship plot into a line plot and colors lines by state. section 11.16

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Demonstrates plotting, index repair, grouping, confidence intervals, and facet layout, but does not develop statistical theory.
Examples
Uses city weather temperatures, daily taxi distance, daily trip counts, and monthly taxi comparisons.
What it assumes
Assumes familiarity with pandas DataFrames, datetime columns, indexing, grouping, and basic Seaborn plotting.
What you can do after
Build it from scratch with Seaborn by selecting axes, choosing line kind, coloring categories, aggregating values, and arranging facets.
How it connects
Connects Seaborn relplot with pandas DataFrame indexing, groupby aggregation, categorical hue, scatter plots, and confidence intervals.

Also explained in

Compare how 6 books explain line plot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.