Tutorial

Data Visualization with Seaborn Line Plot

Published on August 4, 2022
author

Safa Mulani

Data Visualization with Seaborn Line Plot

Hello, folks! In this article, we will be taking the Seaborn tutorial ahead and understanding the Seaborn Line Plot. We recently covered Seaborn HeatMaps so feel free to have a look if you’re interested in learning more about heatmaps.


What is a Line Plot?

Seaborn as a library is used in Data visualizations from the models built over the dataset to predict the outcome and analyse the variations in the data.

Seaborn Line Plots depict the relationship between continuous as well as categorical values in a continuous data point format.

Throughout this article, we will be making the use of the below dataset to manipulate the data and to form the Line Plot. Please go through the below snapshot of the dataset before moving ahead.

In the below dataset, the data variables – ‘cyl’, ‘vs’, ‘am’, ‘gear’ and ‘carb’ are categorical variables because all the data values fall under a certain category or range of values.

While the remaining data column falls under the integer/continuous variables because they carry discrete integer values with them.

Input Dataset:

MTCARS Dataset
MTCARS Dataset

Plotting Your First Seaborn Line Plot

In order to start with Line Plots, we need to install and import the Seaborn Library into the Python environment by using the below command:

Syntax:

pip install seaborn

Once you are done with the installation, import the library to the current working environment and use the functions

Syntax:

import seaborn

For the entire series of Seaborn, we will be using Matplotlib library to plot the data and show it in a proper visualized manner.


Creating Single LinePlot with Seaborn

We can supply discrete values or use data sets to create a Seaborn line plot.

Syntax:

seaborn.lineplot(x, y, data)
  • x: Data variable for the x-axis
  • y: The data variable for the y-axis
  • data: The object pointing to the entire data set or data values

Example 1: Using random data to create a Seaborn Line Plot

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

Year = [2012, 2014, 2016, 2020, 2021, 2022, 2018]
Profit = [80, 75.8, 74, 65, 99.5, 19, 33.6]

data_plot = pd.DataFrame({"Year":Year, "Profit":Profit})

sns.lineplot(x = "Year", y = "Profit", data=data_plot)
plt.show()

In the below line-plot, we can witness the linear relationship between the two data variables – ‘Year’ and ‘Profit’.

Output:

LinePlot Example 1
LinePlot Example 1

Example 2: Using a Dataset to create a Line Plot and depict the relationship between the data columns.

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

data = pd.read_csv("C:/mtcars.csv")
info = data.iloc[1:20,:5]
sns.lineplot(x = "drat", y = "mpg",data=info)
sns.set(style='dark',)
plt.show()

Input Dataset:

Input Dataset Seaborn LinePlot
Input Dataset Seaborn LinePlot

Output:

LinePlot Example 2
LinePlot Example 2

Multiple Seaborn Line Plots

We can create multiple lines to visualize the data within the same space or plots. We can use the same or multiple data columns/data variables and depict the relationship between them altogether.

1. Using the hue Parameter To Create Color Hue for Multiple Data Points

The parameter hue can be used to group the different variables of the dataset and would help depict the relationship between the x and the y-axis data columns with the column passed as a value to the parameter.

Syntax:

seaborn.lineplot(x,y,data,hue)

Example:

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

data = pd.read_csv("C:/mtcars.csv")
info = data.iloc[1:20,:5]
sns.lineplot(x = "drat", y = "mpg", data=info, hue="cyl")
plt.show()

As seen in the below plot, it represents three lines with a different color scheme to depict the relationship between the ‘drat’, ‘mpg’ and ‘cyl’ respectively.

Output:

Multiple Seaborn LinePlot
Multiple Seaborn LinePlot

2. Using the style Parameter to Plot Different Types of Lines

We can set the style parameter to a value that we’d like to display along with the x and the y-axis and also specify different line structures: dash, dots(markers), etc.

Syntax:

seaborn.lineplot(x, y, data, style)

Example 2:

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

data = pd.read_csv("C:/mtcars.csv")
info = data.iloc[1:20,:5]
sns.lineplot(x = "drat", y = "mpg", data=info, hue="cyl", style="cyl")
plt.show()

As seen clearly, the plot represents the ‘cyl’ values in relation with ‘mpg’ and ‘drat’ with different line structures i.e. plain line, dashes and markes.

Output:

Line Plot With style Parameter
Line Plot With style Parameter

3. Using size parameter to plot multiple line plots in Seaborn

We can even use the size parameter of seaborn.lineplot() function to represent the multi data variable relationships with a varying size of line to be plotted. So it acts as a grouping variable with different size/width according to the magnitude of the data.

Syntax:

seaborn.lineplot(x, y, data, size)

Example 3:

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

data = pd.read_csv("C:/mtcars.csv")
info = data.iloc[1:20,]
sns.lineplot(x = "drat", y = "mpg", data=info, hue="gear",style="gear",size="gear")
plt.show()

Input Dataset:

Dataset For Multiple Line Plot
Dataset For Multiple Line Plot

Output:

Line Plot With size Parameter
Line Plot With size Parameter

Using different color palette along with Line Plot

Seaborn colormap and palette define the color range for the visualization models. The parameter palette along with hue can be used for determining the color encoding scheme in terms of the data variable.

For more color palettes, you can reference the link here: Color Palette

Syntax:

seaborn.lineplot(x,y,data,hue,palette)

Example:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
 
data = pd.read_csv("C:/mtcars.csv")
info = data.iloc[1:20,]
sns.lineplot(x = "drat", y = "mpg", data=info, hue="gear", palette = "Set1")
plt.show()

Output:

Line Plot Palette
Line Plot Palette

Addition of Error Bars to Line Plot

Line Plots can be used to define the confidence levels/intervals in the plots to depict the error rates through the use of err_style parameter.

Syntax:

seaborn.lineplot(x,y,data,err_style="bars")

Example:

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

data = pd.read_csv("C:/mtcars.csv")
info = data.iloc[1:20,]
sns.lineplot(x = "cyl", y = "mpg",data=info, err_style="bars")
plt.show()

Output:

Line Plot With err_style Parameter
Line Plot With err_style Parameter

Setting different style using seaborn.set() function

Python seaborn.set() function can be used to display the plot in a different background style.

Syntax:

seaborn.set(style)

Example:

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

data = pd.read_csv("C:/mtcars.csv")
info = data.iloc[1:20,]
sns.lineplot(x = "cyl", y = "mpg",data=info,hue="gear")
sns.set(style='dark',)
plt.show()

Output:

Line Plot With set() function
Line Plot With set() function

Conclusion

Thus, in this article, we have understood the Line Plots and the variations associated with it.

I strongly recommend the readers to go through Python Matplotlib tutorial to understand the Line Plots in a better manner.


References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.