Data Visualization with Line Plots
Learn how to design and interpret line plots as well as bootstrapping techniques used in seaborn library plots.
We'll cover the following...
Overview
Line plots represent continuous relationships between two variables, where time is often one of the variables used and is plotted on the x-axis. Line plots are mainly used for time series data analysis, where we observe the trends and patterns of how a variable changes over time.
Use line plots in Seaborn
We begin by importing the required libraries (seaborn
, matplotlib
, and pandas
) and set the default seaborn styling theme using the sns.set_theme()
function. Next, we import the fmri
dataset using the sns.load_dataset()
function and view the first five records using the pandas head()
method. The dataset has five columns—the column timepoint
represents the time.
import seaborn as snsimport matplotlib.pyplot as pltimport pandas as pdsns.set_theme()fmri_df = sns.load_dataset('fmri')print(fmri_df.head())
Let’s plot a line plot for timepoint
and signal
. We use the sns.lineplot()
function and pass fmri_df
in the data
parameter, timepoint
on the x-axis and signal
on the y-axis. We can also customize the plot with the matplotlib
axis object plt
to set the title
, xlabel
, and ylabel
. We save the figure using plt.savefig()
. The resulting line plot is shown below:
sns.lineplot(data= fmri_df, x='timepoint', y='signal')plt.title("Change in signal over time", fontsize= 10)plt.xlabel("Time point", fontsize= 10)plt.ylabel("Signal", fontsize= 10)plt.savefig('output/graph.png') # save figure
The plot shows that the signal
value peaked at timepoint
5.0 and slowly decreased.
We can further customize the plot by passing the event
column to the hue
parameter and adding the error bars by setting err_style='bars'
in the sns.lineplot()
function. The resulting plot is shown below:
sns.lineplot(data = fmri_df, x='timepoint', y='signal', hue='event',err_style='bars') # add error bars and hueplt.title("Change in signal over time", fontsize= 10)plt.xlabel("Time point", fontsize= 10)plt.ylabel("Signal", fontsize= 10)plt.savefig('output/graph.png') # save figure
Let’s now demonstrate line plots on another dataset named IrishWhiskeySales.csv
. This Irish Whiskey Sales dataset is publicly available online.
We import the dataset in the pandas DataFrame sales_df
. We ...