Reading Time: 4 minutes

[ad_1]

The column passed to hue parameter is used as a grouping column. A single line is now grouped into multiple lines as per the number of categories in that column. And, the groups are shown as different colored lines.

In this post, we will discuss:

  1. How to use hue parameter to get multiple lines?
  2. How to change line colors using color palette?
  3. How to change line colors using list of colors?
  4. How to change line colors using a dictionary of colors? (assign specific color for a specific category)
  5. Plot without shaded region around the line

Example

For this demo, I am using iris dataset. It is available as inbuilt dataset in seaborn.

First, I am importing the libraries. And then, I will load the dataset.

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

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

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

1. How to use hue parameter to get multiple lines?

I am using the column “species” to create groups. I am passing this column to hue parameter. It plots three lines, one for each species, in different colors as shown below.

sns.lineplot(data=iris, 
             x="sepal_length", 
             y="petal_length", 
             hue="species")
seaborn line plot multiple lines - hue
seaborn line plot multiple lines – hue

Below sections cover – customizing the line colors.

2. How to change line colors using color palette?

Choose your favorite color palette from matplotlib colormaps. Go to this page of the documentation – matplotlib color maps. Scroll down and you can find lots of palettes to choose from.

Pass the palette name to the “palette” parameter as shown below. I have used the palette “twilight” in this example. (Note: The palette names are case sensitive).

sns.lineplot(data=iris, 
             x="sepal_length",
             y="petal_length", 
             hue="species", 
             palette="twilight")
seaborn lineplot change line color using color palette
seaborn lineplot change line color using color palette

3. How to change line colors using list of colors?

Instead of a ready-made palette, you can also pick your own colors and pass them as a list to the palette parameter. The available colors can be found in this page of the matplotlib documentation – list of named colors.

Pick a few colors and pass it together as a list. The number of colors you mention should match the number of categories. For example, in this case, the species column has 3 categories – setosa, versicolor, verginica. So I have passed 3 colors.

It is not guaranteed which color is plotted for which category. If you are more picky, and want to use specific color for a specific category, jump to the next section.

sns.lineplot(data=iris, 
             x="sepal_length",
             y="petal_length", 
             hue="species", 
             palette=["forestgreen", "deeppink", "royalblue"])
seaborn lineplot change line color using list of colors
seaborn lineplot change line color using list of colors

4. How to change line colors using a dictionary of colors? – (specific color for each category)

It is possible to assign specific color for specific category by passing it as a dictionary mapping as given below.

(To pick colors: list of matplotlib named colors).

sns.lineplot(data=iris, 
             x="sepal_length",
             y="petal_length", 
             hue="species", 
             palette={"setosa": "royalblue", "versicolor": "deeppink", "virginica": "forestgreen"})
seaborn lineplot assign specific color for category
seaborn lineplot assign specific color for category

You can verify the colors plotted. I have assigned royalblue color for setosa category in the code. Check the plot. Setosa is marked in royalblue. Similarly all categories are marked in their assigned colors.

5. Plot without shaded region around the line

Set ci parameter to None to avoid the shaded area around the line plot.

ci means confidence interval.

sns.lineplot(data=iris, 
             x="sepal_length",
             y="petal_length", 
             hue="species", 
             palette={"setosa": "royalblue", "versicolor": "deeppink", "virginica": "forestgreen"},
            ci=None)
seaborn lineplot without shaded region
seaborn lineplot without shaded region

For complete list of lineplot parameters, please refer to the 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: Photo by Pawel Czerwinski on Unsplash)



[ad_2]

Source link

Spread the Word!