In this tutorial, I’ll show you how to create a Seaborn lineplot with the Seaborn Objects interface.
So this tutorial will show you the syntax for building a lineplot with the Seaborn objects system, and it will show you step-by-step 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:
Alternatively, if you want to understand how to use the sns.lineplot function, we have a separate tutorial on sns.lineplot.
With all that said, let’s start with an introduction.
A quick introduction to the Seaborn Lineplot
As the name implies, a “lineplot” is a plot that contains …
… wait for it …
Lines.
The lineplot (AKA, line chart) is a tool that we commonly use to plot time series data, or some sort of data that changes over time.
In Python, there are several tools for creating lineplots, including the Matplotlib plot()
function and the traditional sns.lineplot()
function.
But in this tutorial, I’ll show you how to use the Seaborn Objects API to create a lineplot.
You can use the Seaborn Objects visualization system to create line charts with a single line:
And you can also use the Seaborn Objects system to create a line chart with multiple lines:
Personally, I think that the Seaborn Objects system is great for this.
Although you can create simple charts with traditional Seaborn and Matplotlib functions, the Seaborn Objects system is much more flexible. It gives you a lot of control over your plots, with very simple syntax.
With that being said, let’s look at the syntax for creating a line chart with the Seaborn Objects system.
The Syntax for a Seaborn Lineplot
Here, I’ll show you the syntax for how to create a lineplot with the Seaborn objects system.
To be clear: this is a new way to create a Seaborn lineplot, as of 2022. If you want a high-level overview of the Seaborn Objects system, you can read our tutorial about the Seaborn Objects API.
The older way to create a lineplot was with the sns.lineplot function. Review that tutorial for the syntax of sns.lineplot.
A quick note: import Seaborn Objects
Before we look at the syntax, I want to remind you that to use the Seaborn Objects package, we need to import it first.
And exactly how we import it will change the syntax slightly.
The common way to import Seaborn Objects is like this:
import seaborn.objects as so
By importing Seaborn Objects like this, we can call the Seaborn Objects functions with the alias so
.
Seaborn Lineplot syntax
Ok. Now, let’s look at the syntax to make a Seaborn lineplot with Seaborn Objects.
We initialize the plot by calling so.Plot()
. Inside the call to the Plot function, there are a few parameters that we use to specify the dataset, and the variable mappings (i.e., which variable to put on the x and y axes).
We also need to call the .add()
method, which tells Seaborn what type of “mark” to draw. To draw a lineplot, we need to call so.Line()
inside of the .add()
method.
The parameters of for a Seaborn Lineplot
In the syntax image above, I noted several parameters that we typically need when we make a lineplot with the Seaborn Objects interface. There are also a few others.
The commonly used parameters that I think you should know are:
data
x
y
color
group
Let’s look at these one at a time.
data
The data
parameter specifies the dataframe with the data that you want to visualize.
Technically, this parameter is optional.
That’s because we can plot data in a dataframe, in which case we’ll use the data
parameter.
But we can also plot data that’s in Numpy arrays. If you’re plotting Numpy arrays, you probably won’t need this paramter.
So if your data is in a dataframe, you can use the data
parameter, and the argument to the parameter will be the name of the dataframe that holds your data.
Additionally, there’s an important point about how you use this. When you pass the name of a dataframe to the data
parameter, the name does not need to be in quotes. (That’s in contrast to some of the arguments to other parameters.)
x
The x parameter allows you to specify a variable that you want to map to the x-axis.
If you’re plotting data in a dataframe (i.e., you used the data
parameter as explained above), then the argument to this parameter will be the name of a column in that dataframe. If your argument to this parameter is a column in a dataframe, you’ll need to have the name of that column inside quotes.
Alternatively, you can also supply the name of a 1-dimensional Numpy array or array-like object to this parameter. If that’s the case, you will refrain from using quotes around the array name. They are not required for a Numpy array.
Note that this parameter appears inside of the call to so.Plot()
.
y
The y parameter allows you to specify a variable that you want to map to the y-axis.
Like the x
parameter, if you’re plotting data in a dataframe (i.e., you used the data
parameter as explained above), then the argument to the y
parameter will be the name of a column in that dataframe. In this case, the name of that column needs to be inside quotes.
Alternatively, you can also supply the name of a 1-dimensional Numpy array or array-like object to this parameter. In that case, quotes will be unnecessary.
Note that this parameter appears inside of the call to so.Plot()
.
color
The color
parameter manipulates the color of the line or lines that appear in the plot.
Note that you can use this parameter inside of the so.Plot()
function, but you can also use it inside of the call to so.Line()
. How and where you use this parameter can cause subtle changes to the plot.
If you use this parameter inside of so.Plot()
, you can supply the name of a variable or dataframe column. Most commonly, the argument will be categorical data, and the different categories will become different lines in the plot, with different colors. I’ll show you an example of this in the examples section.
You can also use this parameter inside of so.Line()
, which will enable you to specify a specific color for the line in your plot. For example, you can use this code to set the color of the line or lines to red:
so.Line(color = 'red')
Remember though, the call to so.Line() appears inside of the .add()
method.
If this feels confusing, don’t worry. I’ll show you an example in the examples section.
group
The group
parameter enables you to create a line chart with multiple lines.
So in some ways, it’s similar to the color
parameter.
But whereas the color
parameter creates a lineplot with lines that have different colors …
The group
parameter creates a lineplot where all of the lines have the same
color.
So typically, the argument to this parameter will be a variable or column name that contains categorical data.
Examples: How to make Line Charts in Python using Seaborn
Now that you’ve learned how the syntax works, let’s look at some examples of how to create a Seaborn lineplot with the Seaborn Objects interface.
The links seen here will take you to each specific example, if you need something specific.
Examples:
- Create a Simple Line Chart with One line
- Change the Color of the Line (e.g. change the color to ‘red’)
- Create a Line Chart with Multiple Lines (different colors)
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 these examples, you’ll need to import some packages and get the data that we’ll work with.
Import Pacakges
First, we need to import the Seaborn Objects subpackage, and also the Pandas package.
You can do that by running the following code:
import seaborn.objects as so import pandas as pd
Remember: when we import seaborn.objects
with the alias so
, we can use so
as a prefix when we call the Seaborn Objects functions.
Get dataset
Next, we need to get the data that we’ll be working iwth.
In these examples, we’ll work with some time-series stock data. Specifically, data about Amazon and Google stock.
To get this data, we’ll use the Pandas read_csv
function. We’ll read data that exists at a URL, and read_csv
will import it as a dataframe.
We’ll also use pd.to_datetime()
to transform the date
column to a proper datetime.
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)
And now, let’s print out the data, so you can see what’s in it:
print(stocks)
OUT:
date open high ... adj_close volume stock 0 2000-01-03 81.500000 89.562500 ... 89.375000 16117600 amzn 1 2000-01-04 85.375000 91.500000 ... 81.937500 17487400 amzn 2 2000-01-05 70.500000 75.125000 ... 69.750000 38457400 amzn 3 2000-01-06 71.312500 72.687500 ... 65.562500 18752000 amzn 4 2000-01-07 67.000000 70.500000 ... 69.562500 10505400 amzn ... ... ... ... ... ... ... 9364 2020-11-30 1781.183960 1788.064941 ... 1760.739990 1823800 goog 9365 2020-12-01 1774.369995 1824.829956 ... 1798.099976 1739000 goog 9366 2020-12-02 1798.099976 1835.650024 ... 1827.949951 1222000 goog 9367 2020-12-03 1824.010010 1847.199951 ... 1826.770020 1227300 goog 9368 2020-12-04 1824.520020 1833.160034 ... 1827.989990 1376100 goog [9369 rows x 8 columns]
You’ll notice that the stock
variable contains the ticker symbol for the two companies that are in the dataset (amzn
for Amazon and goog
for Google).
You can check the unique values of the stock
column as follows:
pd.unique(stocks.stock)
OUT:
array(['amzn', 'goog'], dtype=object)
The data also contains a date
variable, as well as open and close prices.
We’ll be able to plot these data as time series, using a line chart.
EXAMPLE 1: Create a Simple Line Chart with One line
First, we’re going to create a line chart that has only a single line.
This will be a simpler case, so it will be easier to understand.
To do this though, we’ll first need to create a subset of our stock data.
Create data subset of Amazon data
Here, we’ll create a subset of our dataframe that contains only Amazon data.
To do this, we’ll take our stock
dataframe and use the Pandas query method to subset where stock == 'amzn'
.
amazon_stock = stocks.query("stock == 'amzn'")
If you print this data out, or examine the unique values, you’ll see that amazon_stock
only contains stock data for Amazon.
Create Line Plot
Now, we can create our line plot.
I’ll show you the code first, and then I’ll explain.
(so.Plot(data = amazon_stock ,x = 'date' ,y = 'close' ) .add(so.Line()) )
Explanation
If you’re used to the “old” Seaborn system this might seem confusing.
But, once you understand how it works, you’ll realize that it really makes a lot of sense.
We use the so.Plot()
function to initialize plotting.
Inside the Plot
function, we use the data
parameter to specify the dataframe (i.e., amazon_stock
). Then we’re using the x
and y
parameters to map the date
column in the dataframe to the x-axis, and map the close
column to the y-axis.
The .add()
method allows us to add a mark type.
In this case, because we want to create a line chart, we’re going to add a “line” mark. To do this, we use so.Line()
.
So do you understand how this works?
We …
- initialize plotting
- specify the dataframe
- map columns from the dataframe to the aesthetic parameters
- “add” a mark type
It’s different than the single-function way of creating a lineplot with sns.lineplot().
But it’s actually much more flexible.
(To review how the Seaborn Objects system works, you can read our tutorial on the Seaborn Objects API.)
EXAMPLE 2: Change the Color of the Line
Next, we’re going to modify the chart that we created in example 1, and change the color of the line.
You’ll notice that by default, the color of the line is blue.
Here, we’ll change it to red.
In this example, we’ll reuse the amazon_stock
dataset that we created in example 1. So if you need it, you can get the code to create it there.
Now, let’s look at the code to create our line chart with a red line:
(so.Plot(data = amazon_stock ,x = 'date' ,y = 'close' ) .add(so.Line(color = 'red')) )
OUT:
Explanation
Notice that the code for this example is almost identical to the code for example 1.
The only difference is that we used the code color = 'red'
inside of the call to so.Line()
.
Syntactically, so.Line()
, and the parameters inside of that function, control the properties of the line marks that we’re drawing. And the color
parameter obviously controls the color of the line marks that we’re drawing.
So by setting color = 'red'
, we’re telling the Seaborn Objects system to change the color of the line to red.
Note also that you can use any “named color” that Python recognizes. So you can try other colors like ‘navy’, ‘green’, ‘darkred’, or one of the others.
Hexidecimal colors will also work. Those can be a little more complicated though, so I’ll refrain from explaining them here.
EXAMPLE 3: Create a Line Chart with Multiple Lines (different colors)
Now, we’ll use the Seaborn Objects API to create a line chart with Multiple lines.
In this example, we’ll use the stocks
that we retrieved above.
Let’s run the code to create the line plot, and then I’ll explain:
(so.Plot(data = stocks ,x = 'date' ,y = 'close' ,color = 'stock' ) .add(so.Line()) )
OUT:
Explanation
You’ll notice that this is almost identical to the previous examples. There are only a few differences
In this case, we are using a different dataframe, that has data for two socks: amzn
and goog
. We’re specifying the dataframe with data = stocks
.
Like the previous examples, we’re setting x = 'date'
and y = 'close'
to map the date
column to the x-axis and the close
column to the y-axis.
We’re also using .add(so.Line())
to specify that we want to add lines to the plot.
The critical difference is that we’re setting color = 'stock'
. This code maps the color of the lines to the values of the stock
column. Because the stock column contains categorical data with two values, this has the effect of creating a line chart with two lines … one for each category.
It’s pretty simple, once you know how the Seaborn objects system works.
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 or a Plotly line chart?
The short answer is: yes.
For a long time, data visualization in Python has been a mixed bag. Matplotlib was good for some things. Seaborn was good for others. And more recently, Plotly also had it’s uses.
But with the release of Seaborn version 0.12.0, which contains the new Seaborn Objects API, I will probably be recommending Seaborn for most data visualization tasks.
For very simple charts, like simple line charts, you can probably use Matplotlib or the earlier Seaborn functions.
But the new Seaborn Objects API is superior in many ways. It’s easier to use, easier to modify. And it makes it easier to create more complex charts and graphs.
If you’re doing data science and data visualization in Python, I strongly recommend that you use the new Seaborn Objects syntax.
Leave your other questions in the comments below
Do you still have questions about Seaborn lineplots?
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 create a Seaborn lineplot 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:
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???
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.
(so.Plot(data = stocks
,x = ‘date’
,y = ‘close’
,color = ‘stock’
)
.add(so.Line())
)
Is there a way to specify custom line width i.e make amzn width as 2 and goog as 1 ?