devarena logo
Reading Time: 3 minutes


Size parameter is used to group by a particular column. Thus, you get multiple lines each one with different sizes.

Let us discuss the following cases.

  • How to use size parameter to plot multiple lines?
  • Set line size using a list (Method-1)
  • Set line size using a tuple (Method-2)
  • Set line size using a dictionary – assign specific line size for each category (Method-3)

Example

Let us import the necessary libraries. Then import iris dataset for this demo.

import seaborn as sns
sns.set_theme(style="darkgrid")

set_theme” works only in seaborn 0.11 and above. For lower versions, please ignore this line.

iris = sns.load_dataset("iris")
iris.head()
iris dataset sample rows

How to use “size” parameter to plot multiple lines?

I am plotting “sepal_length” in X-axis, “petal_length” in Y-axis. By passing “species” to the size parameter, I get 3 lines grouped by “species” column. I have used ci=None to avoid the shaded region around the lineplot. (ci means confidence interval).

sns.lineplot(data=iris, 
x="sepal_length", 
y="petal_length", 
size="species",
ci=None)
seaborn lineplot multiple lines - size
seaborn lineplot multiple lines – size

Customize line sizes

There are 3 methods by which you can customize the line size. We will see them below.

Method-1: Set line size using a list

Pass a list of sizes to the “sizes” parameter. The number of elements in the list should match the number of categories by which this plot is grouped. Iris dataset has 3 species. So, I am passing 3 values in this list.

(If you need to set specific size for a specific category, check Method-3).

#Method-1: Passing a list of size
sns.lineplot(data=iris, 
x="sepal_length", 
y="petal_length", 
size="species", 
sizes=[2, 3, 4],
ci=None)
seaborn lineplot set line size using list of sizes
seaborn lineplot set line size using list of sizes

Method-2: Set line size using a tuple

Pass two size values in a tuple to the “sizes” parameter. They will behave as lower and upper size limits. Seaborn will determine the sizes for multiple lines based on this range of size.

(If you need to set specific size for a specific category, check Method-3).

#Method-2: Passing a tuple of size
sns.lineplot(data=iris, 
x="sepal_length", 
y="petal_length", 
size="species", 
sizes=(2, 4),
ci=None)
seaborn lineplot set line size using a tuple as size range
seaborn lineplot set line size using a tuple as size range

Method-3: Set line size using a dictionary (assign specific size for specific categories)

To the “sizes” parameter, I am passing a category : size mapping as a dict.

“Setosa”, “versicolor” and “virginica” are 3 different categories of the “species” column by which this plot is grouped. I am assigning size 2 for “setosa“, size 3 for “versicolor”, size 4 for “virginica”. Check the plot legend that these sizes are set as defined.

#Method-1: Passing a dictionary of size
sns.lineplot(data=iris, 
x="sepal_length", 
y="petal_length", 
size="species", 
sizes={"setosa":2, "versicolor": 3, "virginica":4},
ci=None)
seaborn lineplot set line size using a dict
seaborn lineplot set line size using a dict

So far, we have seen the usage of “size” and “sizes” parameters. For complete list of parameters for lineplot, please refer to seaborn documentation here.

Further Reading

Thanks for reading my post. If you find this helpful, please consider following this website on Youtube / Facebook / Twitter / Linkedin.

(Featured Image: Image by Niklas Ernst from Pixabay)





Source link

Spread the Word!