Get the perfect color palette with Seaborn

While seaborn provides much more stylish plots than the 90s-looking matplotlib, I have often struggled to get the right color palette for my data visualisations. Here are some simple one-liners to help you choose the perfect fitting palette for your graphs.

Current palette

# Import library
import seaborn as sns

# Call the current horrible color palette

On the matplotlib site is the list of all pre-defined color palettes. There are dozens of them, discrete or sequential.

Generate a custom discrete palette

To get a palette with n discrete colors, just name it and choose your number of colors:

# Get a palette with n colors
sns.color_palette("autumn", 6)

You can get the list of each color in RGB format:

# Get the list of colors in RGB tuples
[i for i in sns.color_palette("autumn", 6)]
[(1.0, 0.1411764705882353, 0.0),
 (1.0, 0.28627450980392155, 0.0),
 (1.0, 0.42745098039215684, 0.0),
 (1.0, 0.5725490196078431, 0.0),
 (1.0, 0.7137254901960784, 0.0),
 (1.0, 0.8588235294117647, 0.0)]

To extract the color list in hexadecimal format, just add .as_hex():

# Get the list of hex colors
[i for i in sns.color_palette("autumn", 6).as_hex()]
['#ff2400', '#ff4900', '#ff6d00', '#ff9200', '#ffb600', '#ffdb00']

Generate a sequential palette from one color

You can simply create sequential palettes from a single color, including from a HTML color name, with light_palette() and dark_palette():

# Create a light palette
sns.light_palette("firebrick", as_cmap=True)
# Create a dark, reversed palette
sns.dark_palette("#008080", reverse=True, as_cmap=True)