Skip to main content
HomeTutorialsPython

Seaborn Color Palette: Quick Guide to Picking Colors

Use color_palette() for clear categorical separation, cubehelix_palette() for gradual sequential data, and diverging_palette() for clear divergence from a midpoint.
Sep 30, 2024  · 4 min read

In data visualization, selecting appropriate colors can be as important as choosing the right chart type or data representation. Luckily, Python makes it easy to find the right color palette with Seaborn. Read along to understand how to create visualizations that are not only informative but also visually striking and accessible.

If you're new to Python consider starting with either the Introduction to Python course or the Python Programming skill track. These resources will provide you with the necessary skills to follow along with the examples and fully utilize the capabilities of Seaborn.

Quick Note on Color Basics

There are three main elements that define any color: hue, saturation, and value. Each is important because they convey different types of information.

Hue

Hue is what we typically think of as "color" - it's the pure color itself, such as red, blue, or green.

Illustration of hue. Image by Author.

This visual shows different hues, moving through the color wheel from red to purple. Different hues are good for representing categorical data or highlighting specific elements. For example, you might use distinct hues to differentiate between unrelated categories in a bar chart or scatter plot.

Saturation

Saturation refers to the intensity or purity of a color. A fully saturated color is vivid, while a less saturated color appears more muted or gray.

Illustration of saturation. Image by Author.

This gradient shows different levels of saturation for a single hue. The color on the right is fully saturated. As we move left, the color becomes increasingly desaturated, eventually appearing almost gray. More saturated colors can draw attention to key data points or regions of interest.

Value

Value, also known as brightness, refers to the lightness or darkness of a color. Adding white increases the value, while adding black decreases it.

Illustration of value (brightness). Image by Author.

This gradient shows different levels of brightness for the same hue. On the right, we see the color at its brightest. As we move left, the value decreases, with the color becoming darker. Value is useful for showing progression or hierarchy in data. For instance, you might use a value gradient in a heatmap to show the intensity of data across a two-dimensional grid.

Seaborn Color Palette Types

Let's explore the three main types of color palettes in Seaborn: qualitative, sequential, and diverging.

Qualitative palettes using color_palette()

Qualitative palettes are designed for categorical data where there's no inherent ordering or relationship between categories. These palettes use distinct hues to differentiate between categories.

Qualitative palette. Image by Author.

The "Bright" palette shown above is an example of a built-in qualitative palette in Seaborn. It uses distinct, vibrant hues to separate categories. The colors are easily distinguishable from one another, making it ideal for categorical data where there's no inherent order or relationship between categories.

The color_palette() function is Seaborn’s way of creating custom color palettes. It allows you to switch between these different types of palettes by changing the palette name and the number of colors. When using named palettes (like "pastel" or "Blues"), the number parameter determines how many colors you see. Generally, you match the number of colors to your data categories or create a gradient with a specific number of steps.

For even more precise control, you can use HEX codes to define exact colors. This is especially useful when you need to match specific brand colors or create a unique color scheme not available in the built-in palettes. Here's how you can create different types of palettes using color_palette():

import seaborn as sns
# Create a qualitative palette with 4 colors
qual_palette = sns.color_palette("pastel", 4)

Creating a qualitative palette with four colors. Image by Author.

# Create a sequential palette with 6 colors
seq_palette = sns.color_palette("Blues", 6)

Creating a sequential palette with six colors. Image by Author.

# Create a diverging palette with 12 colors
div_palette = sns.color_palette("RdBu", 12)

Creating a diverging palette with 12 colors. Image by Author.

# Create a custom palette using HEX codes 
hex = sns.color_palette(["#FF6B6B", "#4ECDC4", "#45B7D1","#FAD02E", "#594F4F"])

Creating a custom palette with HEX codes. Image by Author.

Sequential palettes using cubehelix_palette()  

Sequential palettes are ideal for numerical data that has a natural ordering. These palettes show progression from low to high values, typically using variations in lightness and/or saturation of a single hue or multiple hues.

Illustration of Viridis sequential palette. Image by Author.

The "Viridis" palette shows a progression from dark purple to bright yellow. This type of palette is good for showing a range of values from low to high. This would be particularly useful for visualizing continuous data like temperature ranges, population densities, or any dataset where you want to show a clear low-to-high relationship. Other built-in options include: ‘Blues’, ‘Greens’, ‘Reds’, ‘YlOrBr’, and ‘rocket’.

The cubehelix_palette() function allows for the creation of sequential palettes with fine-tuned control over brightness and color progression. This function is particularly useful when you need a palette that smoothly increases in brightness and varies slightly in hue. Here are the parameters for customization:

  • n_colors: The number of colors in the palette.

  • start: The starting position in the color cycle (0-3).

  • rot: Determines how much the hue changes. 

  • dark: The darkest color in the palette.

  • light: The lightest color in the palette.

  • reverse: Whether to reverse the direction of the palette.

import seaborn as sns
# Create a default cubehelix palette
default_cube = sns.cubehelix_palette(8)

Illustration of default cubehelix palette. Image by Author.

# Create a custom cubehelix palette
custom_cube = sns.cubehelix_palette(8, start=.5, rot=-.75, dark=.3, light=.8)

Illustration of a custom cubehelix palette. Image by Author.

# Create a reversed cubehelix palette
reversed_cube = sns.cubehelix_palette(8, start=2, rot=0, dark=0, light=.95, reverse=True)

Illustration of a reversed cubehelix palette. Image by Author.

Diverging palettes using diverging_palette()

Diverging palettes are designed for data that has a meaningful midpoint or zero value. These palettes highlight divergence from a central point, using two contrasting hues that become more saturated as values move away from the center. 

Illustration of a diverging palette. Image by Author.

The above example effectively illustrates how these palettes highlight divergence from a central point. The palette transitions from cool blues through neutral white to warm reds, with a clear midpoint. This type of palette could be used to visualize temperature anomalies (where 0 represents the average temperature), or profit/loss scenarios in financial data. Other built-in options include: 'RdBu', 'RdYlGn', and 'PiYG'. Let's look at the parameters:

  • h_neg and h_pos: The hues for the negative and positive extents of the palette.

  • n: The number of colors in the palette.

  • s: The saturation of the colors.

  • l: The lightness of the colors.

  • center: The color of the center point ('light' or 'dark').

import seaborn as sns
# Create a default diverging palette
default_div = sns.diverging_palette(220, 20, n=7)

Illustration of a default diverging palette. Image by Author.

# Create a diverging palette with more saturation
saturated_div = sns.diverging_palette(250, 30, l=65, s=75, n=7)

Diverging palette with more saturation. Image by Author.

# Create a diverging palette with a darker center
dark_center_div = sns.diverging_palette(150, 275, s=80, l=55, n=9, center="dark")

Diverging palette with a darker center. Image by Author.

Final Note on Seaborn Color Palettes

Selecting the right color palette is a critical aspect of creating effective data visualizations. Seaborn's diverse and customizable color palette options provide powerful tools for enhancing the clarity and impact of your data representations.

As you apply these color concepts to your data visualization projects and seek to advance your skills, consider exploring comprehensive learning paths such as the Python Developer or Data Analyst with Python career track. These career-oriented tracks are designed to elevate your proficiency from foundational concepts to more advanced applications, and understanding good visualizations is always important.


Photo of Vinod Chugani
Author
Vinod Chugani
LinkedIn

As an adept professional in Data Science, Machine Learning, and Generative AI, Vinod dedicates himself to sharing knowledge and empowering aspiring data scientists to succeed in this dynamic field.

Seaborn Color Palette FAQs

What are the different types of color palettes available in Seaborn?

Seaborn offers three main types of color palettes: qualitative, sequential, and diverging. Qualitative palettes are ideal for categorical data, sequential palettes for numerical data with a natural ordering, and diverging palettes for data that have a meaningful midpoint.

How can I create a custom color palette in Seaborn?

You can use the color_palette() function to create custom palettes, including qualitative, sequential, and diverging types. It allows for specific color selection using HEX codes for precise control.

When should I use cubehelix_palette() in Seaborn?

The cubehelix_palette() function is useful for creating sequential palettes with smooth color progression, particularly when you need fine-tuned control over brightness and color variation.

What is the purpose of using diverging_palette() in data visualization?

The diverging_palette() function is designed for data with a meaningful midpoint, such as profit/loss or temperature anomalies. It uses contrasting hues to show divergence from the center, helping emphasize deviations.

What are hue, saturation, and value in color selection?

Hue represents the pure color, such as red or blue; saturation indicates the intensity of the color, from vivid to muted; and value, also known as brightness, defines the lightness or darkness of the color. Understanding these elements helps in choosing effective colors for visualizing different types of data.

 
Topics
Related

cheat-sheet

Python Seaborn Cheat Sheet

This Python Seaborn cheat sheet with code samples guides you through the data visualization library that is based on Matplotlib.
Karlijn Willems's photo

Karlijn Willems

2 min

tutorial

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

tutorial

Python Seaborn Line Plot Tutorial: Create Data Visualizations

Discover how to use Seaborn, a popular Python data visualization library, to create and customize line plots in Python.
Elena Kosourova's photo

Elena Kosourova

12 min

tutorial

Python Seaborn Tutorial For Beginners: Start Visualizing Data

This Seaborn tutorial introduces you to the basics of statistical data visualization
Moez Ali's photo

Moez Ali

20 min

tutorial

How to Make a Seaborn Histogram: A Detailed Guide

Find out how to create a histogram chart using the Seaborn library in Python.
Austin Chia's photo

Austin Chia

9 min

tutorial

Python Boxplots: A Comprehensive Guide for Beginners

Create stunning boxplots in Python! This comprehensive beginner’s tutorial covers Matplotlib and Seaborn, helping you visualize and compare data distributions.
Austin Chia's photo

Austin Chia

15 min

See MoreSee More