How to Make a Seaborn Lineplot

In this tutorial, I’ll show you how to use the sns.lineplot function to create a Seaborn lineplot.

I’ll give you a quick introduction to what function does, I’ll explain how the syntax works, and I’ll show you some clear examples.

If you need something specific, just click on any of the links below. Each link will take you to the appropriate section of the tutorial.

Table of Contents:

Ok. Let’s start with an introduction.

A quick introduction to the Seaborn Lineplot

So what does the lineplot function do?

To put it simply, the Seaborn lineplot() function creates line charts in Python using the Seaborn package.

You can use it to create line charts with a single line, like this:

A simple line chart with one red line showing time series data.

But you can also use it to create line charts with multiple lines.

An image showing a lineplot that plots two separate lines, one line for different categories.

This is actually much easier to do with Seaborn than in matplotlib. In fact, Seaborn line charts are easier to create and typically better looking than matplotlib line charts. So typically, if I need to make a line chart in Python, I use Seaborn.

Ok. Now that you have a general overview of what the function does, let’s take a look at the syntax.

The syntax of sns.lineplot

The syntax of sns.lineplot is pretty simple, but there are a few important details that you’ll need to learn.

A quick note

One quick note before we look at the syntax.

Whenever we use a package in our code, like the Seaborn package, we need import it first.

This is important, because how we import the package will have an impact on how we call our functions.

Among Python data scientists, the common convention is to import Seaborn with the alias sns. You can do that with the following code:

import seaborn as sns

When we import Seaborn like this, we can use sns as a the prefix before the function name. You’ll see that just in the next section.

sns.lineplot syntax

Ok. Let’s look at the syntax.

Assuming that we’ve imported Seaborn with the alias sns, we call the function as sns.lineplot().

A picture that explains the syntax of the sns.lineplot function, including the 'data' parameter, as well as 'x' and 'y' parameters.

Then, inside the parenthesis, there are several parameters that control exactly how the function works. Let’s look at those parameters.

The parameters of sns.lineplot

The sns.lineplot function has about a dozen parameters, but there are only a few that we commonly use.

The commonly used parameters that I think you should know are:

  • data
  • x
  • y
  • hue
  • color
  • palette

Let’s look at these one at a time.

data

The data parameter enables you to specify the dataframe that contains the data that you want to plot.

Now, to clarify, the argument to this parameter is typically a Pandas dataframe. That’s almost always how I use the lineplot() function. Having said that, the data parameter will accept other types of inputs. For example, it’s possible to use a Numpy array or a list of arrays as an argument to this parameter.

Keep in mind that this is optional. You don’t necessarily need to specify a dataframe. If you don’t, you can simply pass vectors to the x and y parameters.

Additionally, there’s an important point about the syntax. When you type the name of the dataset as the argument to this parameter, the argument name does not need to be inside of quotations. (This is in contrast to the arguments for x, y, and hue.)

x

The x parameter enables you to specify the variable that you want to map to the x-axis.

If you set a dataframe with the data= parameter, then the argument to the x parameter will be a variable name in that dataframe.

Alternatively, if you don’t use the data parameter, you can specify a 1-dimensional Numpy array or a Python list.

When you pass in a dataframe variable as the argument to this parameter, the variable name should be inside of quotation marks. I’ll show you examples of this in the examples section.

y

The y parameter enables you to specify the variable that you want to map to the y-axis.

Much like the x parameter, the argument can be a dataframe variable name or a Numpy array or list.

You’ll pass in a variable name if you set a dataframe with the data parameter. In that case, the argument to the y parameter will be a variable name in that dataframe. If you pass in a variable name like this, the variable name should be inside quotation marks.

Alternatively, if you don’t use the data parameter, you can specify a 1-dimensional Numpy array or a Python list.

color

You can use the color parameter to change the color of the line, if you have a line chart with only one line.

If you have a line chart with multiple different lines, you should probably use the hue parameter instead. (You’ll see examples of both of these in the examples section.)

Appropriate arguments to the color parameter include Python named colors, but hexidecimal colors will also work.

hue

The hue parameter effectively enables you create a multi-line line chart.

It does this by allowing you to map a categorical variable to the color of the lines.

So effectively, if you have a categorical variable with multiple categories, you can use this parameter to create a line chart with multiple lines. Each line will have a different color.

I’ll show you an example of this in example 3.

palette

You can use the palette parameter to change the color of the lines for a multi-line line chart.

Remember: in the section above about the hue parameter, I noted that you can create a multi-line lineplot by mapping a categorical variable to hue. If you do that, the lineplot() function will create a line chart with multiple lines, and each line will be a different color (you’ll see this in example 3).

When you create a multi-line linechart, Seaborn will set the different line colors to default values, according to a default color palette. Additionally, you create a multi-line linechart, you will not be able to change the color of the lines with the color parameter. Remember that the color parameter only works for single-line linecharts.

So to change the colors of a multi-line line chart, you need to use the palette parameter.

Python has several qualitative color palettes. The ones I recommend for line charts are:

  • hls
  • Dark2
  • Set1
  • tab10

I’ll show you an example of how to use the palette parameter in example 4.

Examples: How to make Line Charts in Python using Seaborn

Ok. Now that we’ve looked at the syntax, let’s take a look at some examples of how to create line charts with the Seaborn lineplot function.

I’ll show you several different examples.

The links below will take you to a specific example, if you need to build a specific kind of line chart.

Examples:

Having said that, there is some code that you’ll need to run before the examples will work.

Run this code first

Before you run the examples, you’ll need to import Seaborn and download the dataset that we’ll be working with.

Import Pacakges

First, we need to import the Seaborn package and also import Pandas.

You can do that by running the following code:

import pandas as pd
import seaborn as sns
sns.set()

As I explained earlier in the syntax section, when we import Seaborn this way, we can use the prefix sns when we call our Seaborn functions.

Here, I’ve also used the sns.set() function, which sets the plot formatting of our charts.

Get dataset

Next, we need to get the data that we’re going to visualize.

The data that we’ll be working with is time-series stock data for two companies: Amazon and Google.

To get his data, you need to run the Pandas read_csv() function, which will retrieve and import a CSV file of the data that I’ve stored on this website.

You can download and import the data by running this code:

stocks = pd.read_csv("https://www.sharpsightlabs.com/datasets/amzn_goog_2000-01-01_to_2020-12-05.csv")
stocks.date = pd.to_datetime(stocks.date)

Let’s print out the stocks dataframe so you can take a look:

print(stocks)

OUT:

           date         open         high          low        close     adj_close    volume stock 
0    2000-01-03    81.500000    89.562500    79.046875    89.375000     89.375000  16117600  amzn  
1    2000-01-04    85.375000    91.500000    81.750000    81.937500     81.937500  17487400  amzn  
2    2000-01-05    70.500000    75.125000    68.000000    69.750000     69.750000  38457400  amzn  
3    2000-01-06    71.312500    72.687500    64.000000    65.562500     65.562500  18752000  amzn  
4    2000-01-07    67.000000    70.500000    66.187500    69.562500     69.562500  10505400  amzn  
...         ...          ...          ...          ...          ...           ...       ...   ...  
9364 2020-11-30  1781.183960  1788.064941  1755.000000  1760.739990   1760.739990   1823800  goog  
9365 2020-12-01  1774.369995  1824.829956  1769.369995  1798.099976   1798.099976   1739000  goog  
9366 2020-12-02  1798.099976  1835.650024  1789.473022  1827.949951   1827.949951   1222000  goog  
9367 2020-12-03  1824.010010  1847.199951  1822.650024  1826.770020   1826.770020   1227300  goog  
9368 2020-12-04  1824.520020  1833.160034  1816.989990  1827.989990   1827.989990   1376100  goog 

If you look carefully, you’ll see that the data has several variables related to stock information, including date, the close price, and the ticker symbol (the stock variable).

Let’s quickly check the unique values of the stock variable using the Pandas unique function:

pd.unique(stocks.stock)

OUT:

array(['amzn', 'goog'], dtype=object)

As you can see, the stock variable has two unique values: 'amzn' and 'goog'.

With these variables and the records in this dataframe, we’ll be able to create a few different types of line charts of stock price over time.

Create data subset of Amazon data

We’ll also create a subset that contains only Amazon data.

To create this dataframe, we’re going to take the stock dataset and subset the rows the dataframe using the Pandas query() method.

amazon_stock = stocks.query("stock == 'amzn'")

If you print this out and do some data inspection to check the unique values of the stock variable, you’ll see that amazon_stock

EXAMPLE 1: Create a Simple Line Chart with One line

First, let’s start out by creating a simple line chart.

Here, we’re going to plot the amazon_stock dataframe to show the Amazon stock price over time.

Let’s run the code and take a look:

sns.lineplot(data = amazon_stock, x = 'date', y = 'close')

OUT:

A line chart made in Python of Amazon stock price over time.

Explanation

So what happened here?

We used the Seaborn lineplot() function to create a line chart of Amazon stock price over time.

Let’s dig in to the syntax though.

We called the function with the syntax sns.lineplot().

We indicated that we want to plot data in the amazon_stock dataframe with the code data = amazon_stock. Remember that the amazon_stock dataframe contains only Amazon stock data.

Then we mapped the date variable to the x-axis with the code x = 'date' and mapped close to the y-axis with y = 'close'. (Remember, close is the closing price of the stock, on any given day.)

The output then is a line chart of the Amazon stock closing price by date.

EXAMPLE 2: Change the Color of the Line

Next, let’s change the color of the line.

As you saw in the previous example, when we create a single-line line chart, the default line color is ‘blue’ (unless you’ve changed your default matplotlib/Seaborn settings.)

In many cases, the default blue color will be fine.

But in some other cases, you may want to change the line color.

This is very easy to do.

To change the line color of a lineplot with a single line, we can use the color parameter.

Let’s take a look:

sns.lineplot(data = amazon_stock
            ,x = 'date'
            ,y = 'close'
            ,color = 'red'
            )

OUT:

An image of a Seaborn line chart, where the line color has been changed to red.

Explanation

This is pretty easy to understand, but let me explain.

The code for this line chart is almost exactly the same as the code for the basic line chart that we saw in example 1.

The major difference is that we used the color parameter to change the color of the line. Specifically, we changed it to red with the code color = 'red'. Notice that the name of the color is enclosed inside quotation marks.

Keep in mind that Python has many other valid colors that you can use. Play around a little bit and find some colors that you like.

One last note about this: you can use this for line charts with a single line, but it won’t work for multi-line line charts.

That being said, let’s look at how to create a multi-line line chart.

EXAMPLE 3: Create a Line Chart with Multiple Lines

Here, we’ll create a line chart with multiple lines. Each line will be a different color.

In this example, we’re going to plot data from the stocks dataframe. Remember that we created this dataframe near the beginning of the examples section. Also remember that this dataframe contains data for two stocks: Amazon and Google.

To plot this data, we’ll use the sns.lineplot() function in a way that’s very similar to example 1 and example 2.

We’re setting data = stocks to indicate that we’re plotting the stocks dataframe. And we’re setting x = 'date' and y = 'close' to map the appropriate variables to the x and y axes.

But to create a chart with multiple lines, we’ll use the hue parameter.

Let’s take a look:

sns.lineplot(data = stocks
            ,x = 'date'
            ,y = 'close'
            ,hue = 'stock'
            )

OUT:

A line chart that shows lines for two different stocks: Amazon in blue and Google in orange.

Explanation

This is very similar to previous examples.

The main difference is that we’re using the hue parameter to create a line chart with multiple lines that have different colors.

Specifically, we set hue = 'stock'.

Remember that the stocks dataframe contains time series data for stock ticker symbols for the stocks.

The stock variable is a categorical variable in that dataframe that records the ticker symbol of the stock record. If we check the unique values for this variable, you’ll see that there are only two values: amzn and goog. These are the stock ticker symbols for Amazon and Google respectively.

When we want to create a line chart with multiple lines, we can map a categorical variable to hue. If we do this, sns.lineplot() will create a separate line for each category of our categorical variable.

So when we set hue = 'stock', Seaborn created two different lines: one line for amzn and one line for goog.

Notice that both lines have different colors … they have different “hues.”

Keep in mind that this is a relatively simple example with 2 lines, but if you have data with more than 2 categories, you can create a line chart with more lines.

EXAMPLE 4: Change the Colors of a Multi-line Line Chart

Finally, let’s modify the multi-line chart that we created in example 3 by changing the colors of the lines.

To do this, we’ll use the palette parameter.

Let’s run the code and then I’ll explain.

sns.lineplot(data = stocks
            ,x = 'date'
            ,y = 'close'
            ,hue = 'stock'
            ,palette = 'hls'
            )

OUT:

A Python line chart made with Seaborn, where we've changed the line colors by changing the color palette.

Explanation

So what happened here?

In this example, we modified the line colors by changing the color palette. To do this, we set the palette parameter to palette = 'hls'. ‘hls’ is the name of a Python color palette.

When we did this, it changed the line colors to colors that are in the ‘hls’ palette.

Python actually has a variety of color palettes. When you’re creating line charts, where each line represents a different category, I recommend that you use a “qualitative” color palette.

Python has a variety of qualitative color palettes, but the ones I recommend for line charts are:

  • hls
  • Dark2
  • Set1
  • tab10

Try these out and see which ones you like.

Again though, Python has a variety of qualitative palettes and other color palettes, so check them out and try them in your plots.

Frequently asked questions about Seaborn Lineplots

Ok. Now that we’ve looked at some examples of Seaborn line charts, let me address a common question.

Frequently asked questions:

Is a Seaborn line chart better than a Matplotlib line chart?

One common question among Python data scientists is about whether they should use Seaborn or Matplotlib to create their charts.

The short answer is: I strongly prefer Seaborn.

The code to create a line chart with Seaborn is simpler compared to the code for a Matplotlib line chart.

Seaborn is easier to use when you’re working with dataframes (matplotlib isn’t really designed to work on dataframes).

Seaborn plots are typically better looking when you use the default settings.

And importantly, for line charts, it’s easier to create a multi-line line chart with Seaborn than with matplotlib.

Overall, for data visualization generally, and for line charts specifically, I strongly recommend that you use Seaborn instead of matplotlib.

Leave your other questions in the comments below

Do you still have questions about the Seaborn lineplot function?

If so, just leave your questions in the comments section below.

If you want to master Seaborn, join our course

In this blog post, I’ve shown you how to use the Seaborn lineplot function to create line charts in Python. But if you need to visualize your Python data, there’s a lot more to learn.

That said, if you’re serious about learning and mastering data visualization in Python, you should join our premium online course, Seaborn Mastery.

Seaborn Mastery is our online course that will teach you everything you need to know about data visualization with the Seaborn package.

Inside the course, you’ll learn:

  • how to create essential plots like bar charts, line charts, scatterplots, and more
  • techniques for creating multivariate data visualization
  • how to add titles and annotations to your plots
  • how to “tell stories with data”
  • learn “how to think” about visualization
  • and much more …

Additionally, when you enroll, you’ll get access to our unique practice system that will enable you to memorize all of the syntax you learn. If you practice like we show you, you’ll memorize Seaborn syntax and become “fluent” in writing data visualization.

You can find out more here:

Learn More About Seaborn Mastery

2 thoughts on “How to Make a Seaborn Lineplot”

  1. Hello, thanks a lot.
    1- “hue” parameter is required for a multi-line chart? If required, its value must be ‘stock’?

    2- ‘y’ is an abbreviation of ‘yellow’ color. When I use them, the output is different. Why???

    Reply
    • In response to question 1:

      No … ‘stock’ is the name of one of the variables in the dataframe. The argument to the hue= parameter should be the name of a variable in the dataframe.

      Reply

Leave a Comment