How to use your own color palettes with Seaborn | by Carolina Bento | Towards Data Science

The other day I was putting together a few visualizations with seaborn, which is a great, super easy-to-use library based on Matplotlib.
Even though I like seaborn’s default styles a lot, because their aesthetics are very clean, one thing I usually like to customize are the colors on the data points.
I tried looking around for an end-to-end example of how to use or create a custom color palette in seaborn, but was having a hard time finding one.
So I decided to use my matplotlib knowledge and gather all the information I could find about seaborn color palettes, to create my own code example/template.
Hope you find it useful.
As usual, you start off with beautiful dummy data 😀
import numpy as npimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = np.array([[1, 3, 'weekday'], [2, 2.5, 'weekday'],[3, 2.7, 'weekend'], [4, 2.8, 'weekend'], [5, 3, 'weekday'], [6, 3.1, 'weekday'], [7, 3, 'weekday'], [8, 3.1, 'weekday'], [9, 3.1, 'weekday'], [10, 3.1, 'weekend']])# Creating a data frame with the raw datadataset = pd.DataFrame(data, columns=['day', 'miles_walked', 'day_category'])
If you want to take a peek at the dataset
print(dataset)
And you’ll see something like this
seaborn and matplotlib have a lot of different color palettes to choose from. In this example I’m going to use the Paired palette.
# Set the color palettesns.set_palette(sns.color_palette("Paired"))# Plot the data, specifying a different color for data points in# each of the day categories (weekday and weekend)ax = sns.scatterplot(x='day', y='miles_walked', data=dataset, hue='day_category')# Customize the axes and titleax.set_title("Miles walked")ax.set_xlabel("day")ax.set_ylabel("total miles")# Remove top and right bordersax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)plt.show()
Using an existing color palette, the only thing you need to do is to set it before calling the plot methods, and any other customization you’d like.
If you’d like to use a particular set of colors, either because they’re your favorite colors or are part of a style-guide you’re using, you can do that too!
# Create an array with the colors you want to usecolors = ["#FF0B04", "#4374B3"]# Set your custom color palettesns.set_palette(sns.color_palette(colors))# And then, from here onwards, it's exactly like the previous example# Plot the data, specifying a different color for data points in# each of the day categories (weekday and weekend)ax = sns.scatterplot(x='day', y='miles_walked', data=dataset, hue='day_category')# Customize the axes and titleax.set_title("Miles walked")ax.set_xlabel("day")ax.set_ylabel("total miles")# Remove top and right bordersax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)plt.show()
If you’d like to use your own color palette across multiple plots you can also use the parameter palette in the seaborn plots, and refer to your custom palette throughout your code.
# Create an array with the colors you want to usecolors = ["#FF0B04", "#4374B3"]# Set your custom color palettecustomPalette = sns.set_palette(sns.color_palette(colors))# Use the parameter palette and use your own palette across all your# plotsax = sns.scatterplot(x='day', y='miles_walked', data=dataset, hue='day_category', palette=customPalette)# Customize the axes and titleax.set_title("Miles walked")ax.set_xlabel("day")ax.set_ylabel("total miles")# Remove top and right bordersax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)plt.show()
This will result in the same plot as above! But now you can use the same color palette by referring to customPalette in any of your seaborn plots.
That’s it! A nice way to customize your plots and make your visualizations more insightful.
Thanks for reading!