Seaborn: Color Palettes

As we know that colours play a very important role in visualizations. It can add more value to the plot. 

In this article, we are going to study seaborn color_palette which we can use for customising our plot according to our choice.
In seaborn, we can also make our own palette which will contain the color of our choices.

Syntax

seaborn.color_palette(palette=None, n_colors=None, desat=None, as_cmap=False)

Parameters:

  • palette: It is used for taking the name of palette as an input.
  • n_colors: It takes the number of colors in the palette.
  • desat: It is used in desaturating the proportion of each color.

Example

Here, the Exercise dataset has been taken from seaborn.

 import seaborn as sns

data=sns.load_dataset('exercise')
data.head(5)

Output

data

Here the grid style has been set to darkgrid.

sns.set_style('darkgrid')

Scatter Plot from the seaborn library is used to plot the data. This plot has been used to see whether color palette gets applied to the Plot successfully or not. You can use any other type of plot of your own choice.

sns.scatterplot(x='id', y='pulse', data=data, hue='time')

Output

scatter_1

From the above output, we can observe that three-color blue, orange and green has been used to represent the hue which is set to the "time" column which is a categorical column in the given datasets. These three color has been taken by default.

sns.color_palette()

Output

colors

The above-given code will give us the output that will show the default color palette that seaborn uses. So as we can observe it has taken blue, orange and green color from the starting to represent the data of the categorical column.

We can also give the name of any particular name color palette to plot these graphs.

sns.scatterplot(x='id', y='pulse', data=data, hue='time',palette='terrain')

Output:

terrain

Now, If we want to check all the names of color palette which is by default available in the seaborn by deliberately passing a faulty palette name in the palette which will give us an error and will display all 170 types of color palette available in seaborn.

sns.scatterplot(x='id', y='pulse', data=data, hue='time',palette='b')#this will show errors

Output

ValueError: 'b' is not a valid value for name; supported values are 'Accent', 'Accent_r', 
'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 
'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 
'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 
'Pastel1','Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 
'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r','RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 
'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 
'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 
'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 
'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'crest', 'crest_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'flare', 'flare_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 
'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 
'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 
'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 
'icefire', 'icefire_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'mako',
 'mako_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 
'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'rocket', 'rocket_r', 'seismic', 
'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'vlag', 'vlag_r', 'winter', 'winter_r'

Note: Now in the above output you can observe that there are some palettes present with "_r". There is nothing but the reversed order of their respective palettes.

sns.color_palette('terrain')

Output

order

sns.color_palette('terrain_r')

Output

reversed