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.
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:
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.
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-axisy
: The data variable for the y-axisdata
: The object pointing to the entire data set or data valuesExample 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:
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:
Output:
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.
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:
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:
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:
Output:
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 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:
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:
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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.