How can I plot multiple lines in Seaborn, and can you provide an example?” 2

How can I plot multiple lines in Seaborn, and can you provide an example?”

Seaborn is a data visualization library in Python that allows for creating aesthetically pleasing and informative plots. It offers various functions for plotting multiple lines in a single plot, making it easy to compare trends and patterns between different categories. For example, the `lineplot()` function in Seaborn can be used to plot multiple lines on a single axis, with each line representing a different category or group in the data. This allows for easy comparison and analysis of the data. An example of plotting multiple lines in Seaborn can be seen by using the `lineplot()` function to visualize the relationship between temperature and sales for different cities. This feature of Seaborn makes it a useful tool for data analysis and presentation.

Plot Multiple Lines in Seaborn (With Example)


You can use the following basic syntax to plot multiple lines on the same plot using seaborn in Python:

import seaborn as sns

sns.lineplot(data=df[['col1', 'col2', 'col3']]

This particular example will create a plot with three different lines.

The following example shows how to use this syntax in practice.

Example: Plot Multiple Lines in Seaborn

Suppose we have the following pandas DataFrame that contains information about the sales made by four different retail stores (A, B, C, and D) during eight consecutive years:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'year': [1, 2, 3, 4, 5, 6, 7, 8],
                   'A': [10, 12, 14, 15, 15, 14, 13, 18],
                   'B': [18, 18, 19, 14, 14, 11, 20, 28],
                   'C': [5, 7, 7, 9, 12, 9, 9, 4],
                   'D': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

   year   A   B   C   D
0     1  10  18   5  11
1     2  12  18   7   8
2     3  14  19   7  10
3     4  15  14   9   6
4     5  15  14  12   6
5     6  14  11   9   5
6     7  13  20   9   9
7     8  18  28   4  12

We can use the lineplot() function in seaborn to create a plot that displays four lines to represent the sales made by each store during each year:

import seaborn as sns

#plot sales of each store as a line
sns.lineplot(data=df[['A', 'B', 'C', 'D']])

 

seaborn plot multiple lines

Each line represents the values for one of the four stores.

The legend in the top left corner shows which color corresponds to which store.

Note that we can also use the palette argument to specify our own colors to use in the plot:

import seaborn as sns

#plot sales of each store with custom colors
sns.lineplot(data=df[['A', 'B', 'C', 'D']], palette=['red', 'blue', 'purple', 'pink'])

Notice that the colors of the lines now correspond to the four colors that we specified in the palette argument.

import seaborn as sns

#plot sales of each store with custom colors
sns.lineplot(x='year', y='value', hue='variable', 
             data=pd.melt(df, ['year']),
             palette=['red', 'blue', 'purple', 'pink'])

Each of the lines are now solid instead of having their own line styles.

Note: If you have trouble importing seaborn in a Jupyter notebook, you may first need to run the command %pip install seaborn.

The following tutorials explain how to perform other common tasks in seaborn:

Cite this article

stats writer (2024). How can I plot multiple lines in Seaborn, and can you provide an example?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-multiple-lines-in-seaborn-and-can-you-provide-an-example/

stats writer. "How can I plot multiple lines in Seaborn, and can you provide an example?”." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-multiple-lines-in-seaborn-and-can-you-provide-an-example/.

stats writer. "How can I plot multiple lines in Seaborn, and can you provide an example?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-multiple-lines-in-seaborn-and-can-you-provide-an-example/.

stats writer (2024) 'How can I plot multiple lines in Seaborn, and can you provide an example?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-multiple-lines-in-seaborn-and-can-you-provide-an-example/.

[1] stats writer, "How can I plot multiple lines in Seaborn, and can you provide an example?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I plot multiple lines in Seaborn, and can you provide an example?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top