11.3 C
London
Thursday, May 22, 2025
HomeSoftware TutorialsPythonHow to Adjust Line Thickness in Seaborn (With Example)

How to Adjust Line Thickness in Seaborn (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the linewidth argument within the lineplot() function to adjust the line thickness in seaborn plots:

import seaborn as sns

sns.lineplot(data=df, x='x_var', y='y_var', linewidth=2)

The greater the value you provide, the thicker the line will be.

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

Example: Adjust Line Thickness in Seaborn

Suppose we have the following pandas DataFrame that contains information about the sales made each day at some retail store:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'day': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                   'sales': [3, 3, 5, 4, 5, 6, 8, 9, 14, 18]})

#view DataFrame
print(df)

   day  sales
0    1      3
1    2      3
2    3      5
3    4      4
4    5      5
5    6      6
6    7      8
7    8      9
8    9     14
9   10     18

We can use the lineplot() function in seaborn to create a line plot with a default line width:

import seaborn as sns

#create line plot with default line width
sns.lineplot(data=df, x='day', y='sales')

To increase the line width, we can use the linewidth argument:

import seaborn as sns

#create line plot with increased line width
sns.lineplot(data=df, x='day', y='sales', linewidth=4)

Notice that the line is much thicker in this plot.

Also note that you can use lw as shorthand for “linewidth” if you’d like:

import seaborn as sns

#create line plot with increased line width
sns.lineplot(data=df, x='day', y='sales', lw=4)

This produces the same result.

Also note that if you create a seaborn plot with multiple lines, the linewidth argument will affect the thickness of each line in the plot.

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

Additional Resources

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

How to Add a Title to Seaborn Plots
How to Change Font Size in Seaborn Plots
How to Adjust the Figure Size of a Seaborn Plot

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories