Introduction to line plots

Introduction to Data Visualization with Seaborn

Erin Case

Data Scientist

What are line plots?

Two types of relational plots: scatter plots and line plots

Scatter plots

  • Each plot point is an independent observation

Line plots

  • Each plot point represents the same "thing", typically tracked over time

Line plot of stock price over time

Introduction to Data Visualization with Seaborn

Air pollution data

  • Collection stations throughout city
  • Air samples of nitrogen dioxide levels

First five rows of air pollution DataFrame

Introduction to Data Visualization with Seaborn

Scatter plot

import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2_mean", 
            data=air_df_mean, 
            kind="scatter")

plt.show()

Scatter plot of average nitrogen dioxide over time

Introduction to Data Visualization with Seaborn

Line plot

import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2_mean", 
            data=air_df_mean, 
            kind="line")

plt.show()

Line plot of average nitrogen dioxide over time

Introduction to Data Visualization with Seaborn

Subgroups by location

First five rows of air pollution by location DataFrame

Introduction to Data Visualization with Seaborn

Subgroups by location

import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2_mean", 
            data=air_df_loc_mean, 
            kind="line",
            style="location", 
            hue="location")

plt.show()

Line plot of average nitrogen dioxide over time per region

Introduction to Data Visualization with Seaborn

Adding markers

import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2_mean", 
            data=air_df_loc_mean, 
            kind="line", 
            style="location", 
            hue="location",
            markers=True)

plt.show()

Line plot with markers added

Introduction to Data Visualization with Seaborn

Turning off line style

import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2_mean", 
            data=air_df_loc_mean, 
            kind="line", 
            style="location", 
            hue="location",
            markers=True,
            dashes=False)

plt.show()

Line plot with markers and solid lines

Introduction to Data Visualization with Seaborn

Multiple observations per x-value

First five rows of air pollution DataFrame per station

Introduction to Data Visualization with Seaborn

Multiple observations per x-value

Scatter plot
import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2", 
            data=air_df, 
            kind="scatter")

plt.show()

Scatter plot of nitrogen dioxide for all stations over time

Introduction to Data Visualization with Seaborn

Multiple observations per x-value

Line plot
import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2", 
            data=air_df, 
            kind="line")

plt.show()

Line plot of nitrogen dioxide for all stations over time

Introduction to Data Visualization with Seaborn

Multiple observations per x-value

Shaded region is the confidence interval

  • Assumes dataset is a random sample
  • 95% confident that the mean is within this interval
  • Indicates uncertainty in our estimate

Line plot of nitrogen dioxide for all stations over time

Introduction to Data Visualization with Seaborn

Replacing confidence interval with standard deviation

import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2", 
            data=air_df, 
            kind="line",
            ci="sd")

plt.show()

Line plot with standard deviation

Introduction to Data Visualization with Seaborn

Turning off confidence interval

import matplotlib.pyplot as plt
import seaborn as sns

sns.relplot(x="hour", y="NO_2", 
            data=air_df, 
            kind="line",
            ci=None)

plt.show()

Line plot with no confidence interval

Introduction to Data Visualization with Seaborn

Let's practice!

Introduction to Data Visualization with Seaborn

Preparing Video For Download...