Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn Color Palette not working appropiate with lineplot

I'm having a little trouble with customizing my colors for a lineplot. I want to show an ensemble of spectras with a sequential color palette. The argument "palette="blues" works fine, but does not accept any appropriate color lists (like "Blues_d"), which do not include any bright colors.

This is a representative graph showing how my plot looks like

Below you can see the code I'm using.

color = (sns.dark_palette("purple"))
sns.set()

ax = sns.lineplot(x="Wavelength", y="Absorption", hue="t (min)", lw=1, data=df1, palette=color, legend="brief")

The problem is, that I get the following error:

ValueError: The palette list has the wrong number of colors.

So the question is: How can I use the lineplot function and using a sequential color palette of blues, reds, or whatever that do not include any bright colors?

I'm using pandas version 0.23.3, matplotlib version 2.2.2 and seaborn version 0.9.0

like image 513
Joscha Kruse Avatar asked Aug 09 '18 09:08

Joscha Kruse


2 Answers

Since you mention the t (min) column in the hue option, you need to know the total number of unique values of the column.

Assume that there are 5 unique values in the column. You, thus, can set the number to the n_colors option of sns.color_palette:

ax = sns.lineplot(x="Wavelength", 
                  y="Absorption", 
                  hue="t (min)", 
                  lw=1, 
                  data=df1, 
                  palette=sns.color_palette('coolwarm', n_colors=5), 
                  legend="brief")
like image 63
Hakan Özler Avatar answered Oct 05 '22 03:10

Hakan Özler


No need to worry about the color count if you set your color palette as a color map by setting "as_cmap" argument to True in sns.color_palette:

ax = sns.lineplot(x="Wavelength", 
                      y="Absorption", 
                      hue="t (min)", 
                      lw=1, 
                      data=df1, 
                      palette=sns.color_palette('coolwarm', as_cmap = True), 
                      legend="brief")
like image 37
Pedro Martinez Avatar answered Oct 05 '22 03:10

Pedro Martinez