avatarAnnette Dolph

Summarize

An Analyst’s Guide to Choosing the Perfect Colors in Seaborn

A Guide to Why Color Matters, Seaborn Color Options, and Using Palettes

Photo by Robert Katzki on Unsplash

When you’re first getting started in Seaborn, you might not give much attention to color selection. However, once you have a solid grasp on the Seaborn basics, you can begin to level up your visualizations by putting some attention and care into the colors you choose.

Seaborn offers a wide range of color options to help you make your plots not only informative but aesthetically pleasing and clear.

In this article, we’ll explore the color choices available in Seaborn, ranging from simple color selection to more complex color palettes.

Part I: Why Color Selection Matters

Before we get into the “how,” it’s worth discussing why getting a good grasp on your color options in Seaborn is worth your time and effort as a data analyst.

Strong data visualizations convey information very quickly, and color is one way you can enhance efficient understanding. Here are just a few reasons why it’s worth your time to select colors thoughtfully.

Improve Clarity and Readability

Deliberate color choices help make visualizations clearer and easily interpretable, aiding in data comprehension and readability.

Create Emphasis and Differentiation

You can use color to highlight important data points or categories, enhancing the viewer’s ability to differentiate between various elements within the visualization. In this way, colors can enhance data storytelling and reduce the viewer’s cognitive load.

Improve Accessibility and Inclusivity

Careful consideration of accessible colors ensures that visualizations are clear and understandable for all viewers, including those who may struggle with distinguishing colors.

While you might think this is not common, color blindness actually affects up to 8% of men and 0.5% of women. That’s roughly 1 in 12 people, so there’s a good chance you’re working with someone who would experience challenges reading your visualizations if you don’t design with this in mind!

Create More Aesthetic Appeal and Engagement

Sometimes half of the battle is getting readers or listeners interested and engaged when you’re presenting data, so aesthetics do matter! Well-chosen color palettes enhance the visual appeal of charts and graphs, increasing audience engagement and interest in the data being presented.

Another aesthetic issue you might encounter is the need for consistency and branding. If you need to follow a company branding guide, you’re going to need to know how to set hex or RGB colors. And if you’re presenting multiple visualizations together, there’s a good chance that using palettes to create consistency is going to improve the look and feel of your entire presentation or report.

Memory and Retention

Colors can also significantly impact memory and retention, helping viewers remember essential data points and patterns within the visualization.

To put it simply, if you’re not putting some thought into selecting and customizing colors in your visualizations, you’re missing a significant opportunity to improve clarity and engagement.

With that in mind, let’s dive into the technical knowledge you need in order to customize colors in Seaborn.

Part 2: Using Colors

Seaborn provides a straightforward way to specify colors when creating your plots. You can use common color names or hexadecimal color codes to set the color of your elements.

You can use the color parameter to set the color of, for example, the scatterplot points or bars in your visualization. Here’s an example of the color parameter in action:

import seaborn as sns
import matplotlib.pyplot as plt

# load sample data
data = sns.load_dataset("iris")

# Basic scatter plot with a single color
sns.scatterplot(x="sepal_length", y="sepal_width", data=data, color="blue")
plt.title("Sepal Length vs. Sepal Width")
plt.show()

The color parameter accepts named colors as well as hex codes and RGB values.

Named Colors

You might be wondering what your options for named colors are. You can use named colors from Matplotlib since Seaborn is built on top of Matplotlib. The Matplotlib documentation has a helpful image with all of the Matplotlib named colors including base colors, Tableau colors, and CSS colors.

Seaborn also accepts the named colors from the xkcd color survey (which is a cool thing to read about if you feel like going down a rabbit hole for a minute!) Here is the list of 954 colors from that survey. I don’t think Seaborn accepts all 954 colors, but it seems to accept a lot of them.

To use the xkcd colors, you must first identify the color you want to use as a string, like this:

# Load data set
data = sns.load_dataset("iris")

# Use a valid xkcd color name in the string
xkcd_color = "xkcd:sky blue"  

# Create the scatterplot with xkcd color
sns.scatterplot(x="sepal_length", y="sepal_width", data=data, color=xkcd_color)
plt.title("Sepal Length vs. Sepal Width")
plt.show()

Hex Colors

Hex colors can be specified using a string representation of the hex code, like “#38761d”.

RGB Values

RGB is a color model used to create a broad spectrum of colors by mixing different intensities of red, green, and blue light. In the context of data visualization, RGB values are useful in specifying colors for various elements in your plots and graphs.

RGB values can be specified using float values in a tuple. For instance, (0.53, 0.81, 0.92) represents a sky blue color in the range of 0 to 1 for the red, green, and blue components.

Here’s how the float values would look in action:

import seaborn as sns
import matplotlib.pyplot as plt

# Load sample data
data = sns.load_dataset("iris")

# Define an RGB color using float values between 0 and 1
rgb_color = (0.53, 0.81, 0.92)  # Replace these values with your preferred RGB color

# Scatter plot with RGB color
sns.scatterplot(x="sepal_length", y="sepal_width", data=data, color=rgb_color)
plt.title("Sepal Length vs. Sepal Width")
plt.show()

Part 3: Built-In Color Palettes

Now that we’ve gotten a good grasp on available colors and how to customize them, let’s discuss color palettes.

Seaborn provides a variety of built-in color palettes that you can easily apply to your visualizations. These palettes are designed to work well together and are visually harmonious. Palettes are also useful if you’re presenting several visualizations and want to create a consistent feel.

To select the best palette for your data, it helps to think about whether the data you want to represent is categorical, numeric/sequential, or diverging.

For Categorical Data

The default color palettes provided by Seaborn are deep, muted, pastel, bright, dark, and colorblind. Here are a few of these palettes:

images by author — generated using Seaborn

As you can see, the hues in each individual palette are very different from one another, making it easier to distinguish between elements in your chart. That’s why these palettes are well-suited to displaying categorical data.

Take a look at how using the “bright” palette makes it easy to distinguish categorical variables:

import seaborn as sns
import matplotlib.pyplot as plt

# Load the built-in Titanic dataset
titanic_data = sns.load_dataset("titanic")

# Use the "barplot" function with the "sex" column from the Titanic dataset
sns.barplot(x="sex", y="survived", hue="class", data=titanic_data, palette="bright")

# Set the title
plt.title("Survival Based on Gender and Class")

# Show the plot
plt.show()
image generated by author

For Numeric Data

For numeric data, the Matplotlib built-in palettes like rocket, mako, flare, and crest work well due to the varying brightness of the colors included. Here are a couple of examples:

images generated by author

Since numeric data is often where you want to show gradual change or sequential data, these palettes make sense for displaying that information.

Here’s an example using the built-in “dots” data set and numerical values, along with the flare palette:

import seaborn as sns
import matplotlib.pyplot as plt

# Load the dots dataset from Seaborn
dots_data = sns.load_dataset("dots")

# Plotting using the 'flare' palette with a line plot
plt.figure(figsize=(10, 6))
sns.lineplot(data=dots_data, x='time', y='choice', hue='align', palette='flare')
plt.title('Choice Over Time')
plt.xlabel('Time')
plt.ylabel('Choice')
plt.legend(title='Alignment')
plt.show()
image generated by author

You can see how this palette works nicely for showing choice over time, with a gradual shift in color.

For Diverging Data

Diverging palettes are good for data where both the high and low values are interesting, such as data represented in heat maps. For this, you can consider palettes like vlag or icefire:

images generated by author

As you can see, the colors around the middle of each palette approach similarity, while the values at either end diverge.

Check out how the use of a diverging color palette creates an extremely clear heatmap:

import seaborn as sns
import matplotlib.pyplot as plt

# Load the flights dataset from Seaborn
flights_data = sns.load_dataset("flights")

# Pivot the data to create a matrix suitable for heatmap
flights_pivot = flights_data.pivot("month", "year", "passengers")

# Create a heatmap using seaborn with icefire palette
plt.figure(figsize=(10, 8))
sns.heatmap(flights_pivot, cmap='icefire', annot=True, fmt='d', linewidths=.5)
plt.title('Passenger Numbers by Year and Month (Icefire Palette)')
plt.show()
image generated by author

To demonstrate the helpfulness of choosing a diverging palette when creating a heat map, check out the same visualization created using the mako palette, which we’ve established is better suited to sequential data:

image generated by author

It’s not illegible, but it’s much harder to read, isn’t it? It is harder to perceive contrast. It also visually de-emphasizes the central values.

Additional Palettes

Seaborn has additional palette options available from Color Brewer (Set2, Paired, Blues, Spectral, etc.) When selecting these palettes, one parameter is data_type, so you can be sure the palette is appropriate for sequential, diverging, or qualitative data. As an added bonus, Color Brewer palettes are designed to be colorblind-friendly. Read more in the Seaborn documentation here.

Seaborn also has access to Circular Color Systems (hls and husl) to generate a range of colors evenly spaced in a circular color system. They are used when more colors are needed than are available in the default color cycle.

And, finally, you can also use Cubehelix palettes that generate sequential palettes with a linear increase or decrease in brightness and continuous variation in hue.

Part 4: Custom Color Palettes

If you’ve made it this far, it might be hard for you to imagine why you’d ever need to choose a custom palette since there are already so many options! But, there could be cases where you need to follow a branding guide or want your own colors defined in a palette for some other reason.

To create a custom color palette, you can use the sns.color_palette() function:

import seaborn as sns
import matplotlib.pyplot as plt

# Load the iris dataset from Seaborn
iris_data = sns.load_dataset("iris")

# Define custom colors (replace these with your specific colors)
custom_palette = ["#FF5733", "#33FF57", "#3357FF"]

# Set the custom color palette
sns.set_palette(sns.color_palette(custom_palette))

# Create a scatterplot using the custom colors
sns.pairplot(iris_data, hue="species", height=2.5)
plt.title('Iris Dataset with Custom Color Palette')
plt.show()
image generated by author

When defining a custom palette, you can control the number of colors and customize their order according to your needs.

It’s worth remembering some of the palette selection principles we’ve already discussed even when defining your own palette: think about your data type and what will make the visualizations easiest to read.

Conclusion

Selecting the right colors and color palettes is an essential aspect of creating effective and visually appealing data visualizations with Seaborn.

Seaborn provides a wide array of options to cater to your specific needs. Experiment with different color choices to create informative and engaging data visualizations that convey your message effectively.

In an upcoming article, I will delve into Seaborn’s themes and how they can further enhance the style and aesthetics of your plots. Stay tuned and be sure to follow me for more tips and tricks on data visualization with Seaborn!

Agree with these thoughts? Let me know! Suggestions or corrections to improve this article? Please share them with me!

I’m a former English professor and current administrator who directs an analytics team. I write about what I’ve learned on my self-taught journey to develop my data analysis skills. Through my articles, I hope to help you build your data literacy skills, learn some tips and tricks for Python, SQL, Tableau, Excel, and other common technologies analysts encounter, and think about the ways that data can help both you and your organization grow. Along the way, I’ll share strategies for developing the right mindset and approach for teaching yourself new skills — some drawn from my past teaching experience, and others gathered the hard way from stumbling here and there myself.

The contents of external submissions are not necessarily reflective of the opinions or work of Maven Analytics or any of its team members.

We believe in fostering lifelong learning and our intent is to provide a platform for the data community to share their work and seek feedback from the Maven data fam.

Happy learning!

-Team Maven

Data
Data Science
Data Visualization
Python
Data Analysis
Recommended from ReadMedium