Learn

Seaborn makes it easy to customize which set of colors it uses in its plot functions. There are premade sets of colors to choose from as well as the flexibility to design our own.

Palette options

Seaborn includes six qualitative color palettes: deep, muted, pastel, bright, dark, and colorblind. These palettes are six versions of the same set of colors that are in no specific order, making them a good choice for categorical data groups.

Image showing the six colors palettes available in seaborn as rows of six squares in each color.

However, if we have ordered data, we may be better off with a choice between two types of ordered palettes:

  • Sequential palettes change from lighter to darker colors in one direction. These palettes may be a good choice for ordered categories or continuous numeric scales. Ten squares arranged in a horizontal line that progressively change from white to blue.
  • Diverging palettes generally include a sequence over two colors where increments change from dark to light in the center to dark again at the end. These palettes may be a good choice to show absolute values for data that include both negative and positive values, or data where two things are opposites, like political parties or favorable and unfavorable outcomes. Ten squares arranged in a horizontal line that progressively change from blue to white to orange.

For any of the three palette types, we can choose from seaborn, matplotlib, or Color Brewer included palettes.

Custom palettes

We can also name and create a custom palette with sns.color_palette(). For example, to make a sequential color palette of blues from light to dark in 10 increments, we can use the following code:

seqBlues = sns.color_palette("light:blue", n_colors=10)

We can make our seqBlues palette into a matplotlib continuous colormap by swapping n_colors=10 for as_cmap=True. Other seaborn palette functions with more specific objectives can also be used for palette creation, such as sns.light_palette() or sns.diverging_palette().

Applying palettes

We can change a plot’s palette by setting the palette parameter to the palette’s name. Premade palette names are given in quotes, while custom palettes are given without quotes.

# apply a premade palette sns.barplot(data=df, x='week', y='sales', palette='dark') # apply a custom palette sns.barplot(data=df, x='week', y='sales', palette=seqBlues)

Finally, we can apply settings to all plots by running a function at the start of our notebook:

  • sns.set_palette() applies the same color palette to all plots.
  • sns.set_theme() sets the same plotting parameters to all plots, including features like borders, grids, and background color.

Let’s start trying out some color palettes in our plots! You may want to reference the seaborn documentation on color palettes as you are working.

Instructions

1.

The provided code will create a line plot of plant height over time with one line for each of five plants. Run the code to view the plot with the default color palette.

2.

Because the plants are numbered, seaborn defaults to a sequential palette where the lines become a darker color as the value of Plant increases from 1 to 5. Let’s make a custom sequential palette called plantpal that blends orange to blue over five colors. Note that to create a blended sequential palette, we need to use 'blend:orange,blue' as the first argument of the sns.color_palette() function. We have provided an extra line of code to preview your custom color palette.

3.

Repeat the line plot from step 1 but use plantpal as the palette instead of the default.

4.

The lines are easier to tell apart in these colors, but perhaps a sequential palette isn’t the best option for this plot. Even though the plants are numbered, each number is just a name for the plant and not a true measure of anything (e.g., plant 5 is not greater than plant 4 in some way). Let’s change to the colorblind palette to improve the differentiation of the lines and to remove the implication that the numbers indicate an increasing value.

Repeat the line plot using seaborn’s colorblind palette.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?