Lineplot using Seaborn in Python
Last Updated :
21 Oct, 2021
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides default styles and color palettes to make statistical plots more attractive. It is built on the top of the matplotlib library and is also closely integrated into the data structures from pandas.
Lineplot
Visual representation of a dataset must be chosen according to the dataset or the type of answer we want from the plot. Scatter plots are highly preferred for visualizing statistical relationships. But when it comes to data which is varying with time (or continuous variable), scatter plots are not a good choice. Instead, in Seaborn, lineplot() or relplot() with kind = ‘line’ must be preferred. Line plots give annotation to each of the points and plus helps in customizing markers, line style, and legends.
Syntax: seaborn.lineplot(x=None, y=None, hue=None, size=None, style=None, data=None, palette=None, hue_order=None, hue_norm=None, sizes=None, size_order=None, size_norm=None, dashes=True, markers=None, style_order=None, units=None, estimator=’mean’, ci=95, n_boot=1000, seed=None, sort=True, err_style=’band’, err_kws=None, legend=’brief’, ax=None, **kwargs)
Parameters:
x, y: Input data variables; must be numeric.
data: Dataframe where each column is a variable and each row is an observation.
size: Grouping variable that will produce lines with different widths.
style: Grouping variable that will produce lines with different dashes and/or markers.
Example 1: Let’s take an example of FMRI dataset. It is an example of time-series data, where variables are a function of time. This dataset is in-built available and can be accessed using load_dataset() and needs not to be downloaded separately.
Python3
import seaborn as sns
fmri = sns.load_dataset( "fmri" )
|
There can be multiple measurements of the same variable. So we can plot the mean of all the values of x and 95% confidence interval around the mean. This behavior of aggregation is by default in seaborn.
Python3
sns.lineplot( x = "timepoint" ,
y = "signal" ,
data = fmri);
|
Output-

Example 2: Creating multiple line plot using hue parameters.
Python3
sns.lineplot( x = "timepoint" ,
y = "signal" ,
hue = "region" ,
data = fmri);
|
Output:

Example 3: Creating multiple line plots using style parameters.
Python3
sns.lineplot( x = "timepoint" ,
y = "signal" ,
hue = "region" ,
style = "event" ,
data = fmri);
|
Output:

Example 4: Creating multiple line plots using size parameters. We can use size attributes to change the line pattern.
Python3
sns.lineplot( x = "timepoint" ,
y = "signal" ,
size = "event" ,
data = fmri);
|
Output:

Example 5: Grouping data points on the basis of category, here as region and event. We can add them as hue and style semantics. This will change the line pattern by default.
Python3
sns.relplot( x = "timepoint" ,
y = "signal" ,
hue = "region" , style = "event" ,
kind = "line" , data = fmri);
|
Output:

Example 6: We can also represent standard deviation instead of confidence interval at each time point, for a large dataset, as it can be very time-consuming.
Python3
sns.relplot(x = "timepoint" , y = "signal" ,
kind = "line" , ci = "sd" ,
data = fmri);
|
Output –

Example 7: We can use style and hue with different columns in a data set.
Python3
sns.lineplot(x = "timepoint" , y = "signal" ,
hue = "subject" ,style = "event" ,
data = fmri);
|
Output:

Example 8: Using palette attributes
Python3
sns.lineplot( x = "timepoint" , y = "signal" ,
hue = "subject" , style = "event" ,
palette = "YlOrRd_r" , data = fmri);
|
Output:

Example 9: Adding error bar in line plot using err_style attributes.
Python3
sns.lineplot( x = "timepoint" ,
y = "signal" ,
data = fmri,
err_style = "bars" );
|
Output:

Similar Reads
Grid Plot in Python using Seaborn
Grids are general types of plots that allow you to map plot types to grid rows and columns, which helps you to create similar character-separated plots. In this article, we will be using two different data sets (Iris and Tips) for demonstrating grid plots Using Iris Dataset We are going to use the I
4 min read
seaborn.lineplot() method in Python
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. The colors stand out, the layers blend nicely together, the contours flow throughout, and the overall package not only has a nice aesthe
2 min read
Barplot using seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas. Sea
6 min read
Boxplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas. B
5 min read
Streamline Plots in Plotly using Python
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
2 min read
Splitting Violin Plots in Python Using Seaborn
A violin plot is a data visualization technique that combines aspects of a box plot and a kernel density plot. It is particularly useful for visualizing the distribution of data across different categories. Sometimes, it can be helpful to split each violin in a violin plot to compare two halves of t
5 min read
Stripplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on top of the matplotlib library and also closely integrated into the data structures from pandas. S
5 min read
Swarmplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of the matplotlib library and also closely integrated into the data structures from panda
6 min read
3D Line Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
Simple Plot in Python using Matplotlib
Matplotlib is a Python library that helps in visualizing and analyzing the data and helps in better understanding of the data with the help of graphical, pictorial visualizations that can be simulated using the matplotlib library. Matplotlib is a comprehensive library for static, animated and intera
5 min read