Custom Color Palettes for Seaborn Plots
Being in data science, I’ve always been drawn to creating compelling visuals. It is likely due to being a photographer prior to my data science career. I have gained a profound appreciation for visual storytelling. Creating compelling visuals and communicating your process/results/recommendations is just as important as the analysis itself.
One of the most important tools for compelling visuals is the use of color. Let’s explore the color customization options with two of the most used visualization libraries in python, matplotlib, and seaborn.
Default Plots
If you were like me, you shuddered at the sight of your first plot in python using matplotlib, As I recall, we loaded in our libraries and the classic iris dataset to play around with…
Checked out a simple scatter plot using matplotlib to get started only to find…
Maybe my reaction wasn’t quite this animated but still, I expected more than the flat, boring, scatter plot that I had just created.
I wanted to change everything. It started to get a little better when we switched to seaborn for plotting, a data visualization library built on top of matplotlib.
Color. We might be onto something here. More, please.
Even better. With a little bit of color, we’ve taken a common scatter plot and made it more visually appealing while conveying added insight into our data. We can see the iris data points organized in their respective species colors.
We can alter the colors using several stock palettes and “cmaps” offered in seaborn and matplotlib. Let’s try something different with our plot here.
Plasma! Not a bad way to add some spice to our scatter plot. “Plasma'“ is one of the built in colormaps from the matplotlib library. Having been built on top of matplotlib, seaborn can make use of these colormaps.
A variety of colormaps are available for different situations and analysis. What if the default colors don’t meet our needs though? What if we’d like to customize and fine-tune a palette of colors for our needed visualizations? Perhaps our company, client, team, or presentation calls for a specific color palette.
Seaborn Color Widgets
We won’t go into all the custom color palettes built into matplotlib and seaborn. The purpose of this was to showcase a few unique seaborn widgets for choosing your own color palettes. These options must be used inside of a jupyter notebook in order to function.
ColorBrewer Palette Widget
Looking for a simple color map but don’t know all the names of the matplotlib cmaps? Seaborn includes palettes from colorbrewer2.org. SImply call up seaborn_colorbrewer_palette with either the sequential, diverging, or qualitative options and a widget will appear ready for customization!
Based on your choice, the widget will include a drop-down list of palette options. From here, you can also choose the number of colors in the palette, the saturation level, and the order (regular or reverse) of the output.
Light & Dark Palette Widgets
Looking for more customization? These two widgets can give you more options for sequential color palettes. Call up the widget in your jupyter notebook and options will appear to modify the hue, satuation, luminance, and number of colors in your palette.
Diverging Palette
Need a palette that’s non-sequential? Perhaps your heatmap is in need of your own personal flare? Call up the diverging palette widget and you’re presented with more options. These include your starting hue (the negative side), ending hue (positive side), saturation, luminance, separation, number of palette colors, and whether you’d like the center of the palette to be light or dark.
Cubehelix Palette Widget
The last seaborn widget calls up even broader customization options based on cubehelix palettes. Options in this widget are:
number of colors
starting position in the RGB color space (0 = blue, 1 = red, 2 = green)
number of rotations through the rainbow
gamma correction for intensities (low gamma emphasizes low intensities and vice-versa)
hue (which is actually modifying the satuation in this widget)
lightness values
darkness values
a reverse option to flip the order of the palette
Combining Your Palette & Plot
So, how do we use our palette in our actual plots? Simply save the widget to a variable! Let’s call it my_palette and try it in our original scatter plot!
Take note though, in your jupyter notebook, the widget and color options will reset each time the cell is run. So, set your variable first, adjust your colors, then call the variable in your plot in another cell. Voila!
Just Right
It’s not the most mind-blowing of scatter plots but is has improved by some quick adjustments to one of the most important features in visualizations, COLOR.
While it is possible to manually customize every single color in your charts, doing so can be time consuming, especially with trial-and-error searching for what colors go well together. It is also hard to memorize all of the available colormap options within matplotlib.
The seaborn color widgets offer a nice middle ground to quickly customize your own colors and set them as a new palette for more interesting and useful visualizations.