Introduction to line plots

Introduzione alla visualizzazione dei dati con Seaborn

Content Team

DataCamp

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

Introduzione alla visualizzazione dei dati con Seaborn

Air pollution data

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

First five rows of air pollution DataFrame

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con Seaborn

Subgroups by location

First five rows of air pollution by location DataFrame

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con Seaborn

Multiple observations per x-value

First five rows of air pollution DataFrame per station

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con 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

Introduzione alla visualizzazione dei dati con 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",
            errorbar="sd")

plt.show()

Line plot with standard deviation

Introduzione alla visualizzazione dei dati con 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",
            errorbar=None)

plt.show()

Line plot with no confidence interval

Introduzione alla visualizzazione dei dati con Seaborn

Let's practice!

Introduzione alla visualizzazione dei dati con Seaborn

Preparing Video For Download...