Seaborn Line Plot – Create Lineplots with Seaborn relplot

  • by

In this tutorial, you’ll learn all you need to know about the Seaborn line plot, using the lineplot and replot functions. We’ll explore the sns.lineplot() and sns.relplot() function and provide examples of how to customize your line chart.

Check out the sections below If you’re interested in something specific. If you want to learn more about Seaborn, check out my other Seaborn tutorials, like the bar chart tutorial.

Table of Contents

What are Line Plots?

Line plots are generally used to show how information changes over time. Generally, the data in the x-axis represents a continuous variable (such as dates) and is sorted in a meaningful way.

This all sounds quite similar to a scatterplot – and that’s true. What separates a line plot from a scatterplot is that lines are used to connect the dots to help in the visualization and interpretation of the data.

Difference between relplot and lineplot

Seaborn has two different functions that allow you to create line plots – it gives you the option of using the sns.relplot() function, similar to a scatterplot, or a dedicated sns.lineplot() function to simplify your coding.

As previously mentioned, the line plot is not much different from a scatterplot, except that it uses lines to connect the dots to aid in interpreting the data.

Making a simple Seaborn line plot

Let’s start off by loading some data. I have created a dataset that provides you with the opening stock prices of Apple, during the course of a year long period. Let’s import our libraries and import the data into a Pandas dataframe:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = read_csv('https://raw.githubusercontent.com/datagy/mediumdata/master/AAPL.csv', parse_dates=['Date'])

print(df.head())

Printing this out returns the following dataframe:

        Date       Open Symbol
0 2020-03-30  62.685001   AAPL
1 2020-03-31  63.900002   AAPL
2 2020-04-01  61.625000   AAPL
3 2020-04-02  60.084999   AAPL
4 2020-04-03  60.700001   AAPL

We can see that we have a date variable, an opening price, and a symbol column.

Remember, we can use either the sns.relplot() function or the sns.lineplot() function to create our line chart. Let’s start things off with the sns.relplot() function:

sns.relplot(data=df, x='Date', y='Open', kind='line')

plt.show()

This returns the following image:

Seaborn relplot line

Note that here we had to specify that we wanted Seaborn to create a kind='line' plot.

The benefit of the sns.lineplot() function is that we don’t need to specify this.

Let’s try creating the same chart with the other function now:

sns.lineplot(data=df, x='Date', y='Open')

plt.show()

This returns the following image:

Seaborn lineplot

Now, these plots aren’t exactly inspirational. Let’s make our plots a little prettier by using built-in styling options and colours.

Style your Seaborn line plot

Seaborn makes making your charts prettier a lot simpler and easier than base Matplotlib. Let’s use both the set_palette() function and the set_style() function. You can learn more about these in my other Seaborn introduction tutorial.

We’ll use these functions to apply the darkgrid style and the Set2 color palette:

sns.set_style('darkgrid')
sns.set_palette('Set2')

sns.relplot(data=df, x='Date', y='Open', kind='line')

plt.show()

Now our graph looks much prettier!

Seaborn line plot style

Add title and axis labels to Seaborn line plots

We can use Matplotlib to add a title and descriptive axis labels to our Seaborn line plot.

Let’s explore how we can do this with the code below:

sns.set_style('darkgrid')
sns.set_palette('Set2')

sns.relplot(data=df, x='Date', y='Open', kind='line')

plt.title('Open Price by Date')
plt.xlabel('Date')
plt.ylabel('Open Price')

plt.show()

This returns the following image:

Seaborn line plot add title

Create a Seaborn line plot with multiple lines

Pandas gives you two different options of including multiple lines in your Seaborn line plots. It lets you work with either wide or long data, and the syntax of your functions will look slightly different depending on the format of your data.

Before we dive into creating your plots, let’s recap the differences between wide and long data.

You can find a sample of both a long and a wide dataframe by following the two links below.

Let’s load the wide dataset first:

df_wide = pd.read_csv('https://raw.githubusercontent.com/datagy/mediumdata/master/stocks-wide.csv', parse_dates=['Date'])

print(df_wide.head())

This returns the following dataframe. You can see that the features of the data (the different stock tickers) are displayed as columns.

        Date       AAPL         GOOG        TSLA
0 2020-03-30  62.685001  1125.040039  102.052002
1 2020-03-31  63.900002  1147.300049  100.250000
2 2020-04-01  61.625000  1122.000000  100.800003
3 2020-04-02  60.084999  1098.260010   96.206001
4 2020-04-03  60.700001  1119.015015  101.900002

This is quite different from a long dataset, where the dimension is displayed as its own column (to identify the stock ticker), while the values are in their own column:

df_long = pd.read_csv('https://raw.githubusercontent.com/datagy/mediumdata/master/stocks.csv', parse_dates=['Date'])

print(df.head())

This returns the following dataframe:

        Date       Open Symbol
0 2020-03-30  62.685001   AAPL
1 2020-03-31  63.900002   AAPL
2 2020-04-01  61.625000   AAPL
3 2020-04-02  60.084999   AAPL
4 2020-04-03  60.700001   AAPL

Working with wide data

Seaborn has some ability to work with long data. If we load the dataframe in, Seaborn will infer how to separate your data. Because of this, we don’t actually have to specify what to use, beyond our data.

This might seem like a benefit, but it does remove some flexibility in terms of actually working with realistic datasets.

Let’s give it a shot to see what we need to do:

df_wide = pd.read_csv('https://raw.githubusercontent.com/datagy/mediumdata/master/stocks-wide.csv', parse_dates=['Date'])

sns.lineplot(data=df_wide)

plt.title('Opening Prices')
plt.xlabel('Date')
plt.ylabel('Opening Price')

plt.show()

This returns the following:

Multi lines Seaborn

Now let’s see how to work with a long dataset!

Working with long data

When you’re working with long data, you use the hue= parameter to pass in how you want Seaborn to discern your data. One of the benefits of this approach is that it gives you flexibility to work with different and broader datasets.

Let’s try this approach:

df_long = pd.read_csv('https://raw.githubusercontent.com/datagy/mediumdata/master/stocks.csv', parse_dates=['Date'])

sns.lineplot(data=df_long, x='Date', y='Open', hue='Symbol')

plt.title('Opening Prices')
plt.xlabel('Date')
plt.ylabel('Opening Price')

plt.show()

This returns the following image:

Seaborn Line Plot Multiple lines

Conclusion

In this post, you learned how to create line plots in Seaborn. In particular, you learned how to work with Pandas to create Seaborn line plots, style your line plots, and work with long and wide datasets to produce multi line plots with Seaborn.