Select Page

2.13 Palettes in Seaborn


Here is a look at seaborn’s default color palette:

Now that you have seen this default seaborn palette, you can see the “method to the madness” behind the assignment of colors in a plot containing categorical variables.  In the plot below, which depicts the six levels of the ‘Spec_Event’ variable, the first six colors from the palette above are used, in order.  Note that although bar plots are more typically oriented vertically, we can flip the orientation simply by passing the variable in with the ‘y’ parameter, rather than the ‘x’.  Seaborn will allow us to do the same thing with box plots.

Seaborn has several built-in palette options, as indicated by the list below.

The three main palette types are qualitative, sequential, and diverging.  Qualitative palettes are most typically used when plotting categorical data, for which each level is distinct from the others, and the color scheme should reflect this uniqueness.  Sequential palettes are more commonly used with numeric data, in which a color gradient can reflect the relative intensity of some value.  Diverging palettes are also typically used with numeric data, and work well when there are interesting, extreme values at ends of a spectrum – these are reflected with starkly different colors in a diverging palette, whereas values in the middle may be depicted with blander, less-intense colors.  

Any of these palettes can be viewed by inserting the palette’s name, inside quotation marks, to seaborn’s color_palette() function.  Let’s take a look at ‘icefire’:

By default, icefire only shows us six colors; however, we can modify that with one parameter adjustment in the color_palette() function:

Glancing through the list above, you will notice that several palette names are followed by other palettes of the same name, with ‘_r’ attached at the end.  This suffix indicates a reversal of color order.

We can set these by using seaborn’s set_palette() function.  Alternatively, we can specify a palette by using the ‘palette’ parameter within any seaborn function to change the color scheme.  Let’s try it here with the Special Events barplot, using a couple of the choices from the list above:

Seaborn’s color_palette() function returns the current palette.  

Users can also customize their own palettes.  

In the code cell below, we first define a custom_palette variable as a list of colors, and then use this palette by passing the ‘palette’ parameter to the plot function.

Seaborn’s blend_palette() creates a diverging palette automatically, after a user passes a list of colors to the function.

Seaborn’s light_palette() creates a sequential palette, with light colors that progressively become darker.

Seaborn’s dark_palette() also creates a sequential palette, but with dark colors that progressively become lighter.

If we want to continue to use a particular palette, without having to specify it each time, we can simply pass it to seaborn’s set_palette() function, as shown below.