Set custom seaborn color palette using hex codes, and name the colors

10,111

Use custom function

As commented you may create a function that if called with a custom colorname returns the hex color from the list.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Wanted palette details
enmax_palette = ["#808282", "#C2CD23", "#918BC3"]
color_codes_wanted = ['grey', 'green', 'purple']

c = lambda x: enmax_palette[color_codes_wanted.index(x)]

x=np.random.randn(100)
g = sns.distplot(x, color=c("green"))

plt.show()

Use the C{n} notation.

It should be noted that all colors in seaborn are matplotlib colors. One option matplotlib provides is the so called C{n} notation (with n = 0..9). By specifying a string like "C1" you tell matplotlib to use the second color from the current color cycle. sns.set_palette sets the color cycle to your custom colors. So if you can remember their order in the cycle you can use this information and just specify "C1" for the second color.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Wanted palette details
enmax_palette = ["#808282", "#C2CD23", "#918BC3"]

sns.set_palette(palette=enmax_palette)

x=np.random.randn(100)
g = sns.distplot(x, color="C1")

plt.show()

Manipulate the matplotlib color dictionary.

All named colors are stored in a dictionary you can access via

matplotlib.colors.get_named_colors_mapping()

You may update this dictionary with your custom names and colors. Note that this will overwrite existing colors with the same name.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import seaborn as sns

# Wanted palette details
enmax_palette = ["#808282", "#C2CD23", "#918BC3"]
color_codes_wanted = ['grey', 'green', 'purple']
cdict = dict(zip(color_codes_wanted, [mcolors.to_rgba(c) for c in enmax_palette]))

mcolors.get_named_colors_mapping().update(cdict)
x=np.random.randn(100)
g = sns.distplot(x, color="green")

plt.show()

All codes shown here will produce the same plot in "the company's green" color:

enter image description here

Share:
10,111
Sean McCarthy
Author by

Sean McCarthy

Chief Data Scientist at IJACK Technologies Inc. I love economics, finance, data science, and software. https://mccarthysean.dev https://www.linkedin.com/in/seanmccarthy2/

Updated on June 21, 2022

Comments

  • Sean McCarthy
    Sean McCarthy almost 2 years

    My company has a formal color palette so I need to use these colors in my seaborn charts. I would therefore like to set the default seaborn color palette, and give these colors easy-to-use names such as 'p' for purple and 'g' for green.

    Here is what I have so far for code:

    # Required libraries
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    # Wanted palette details
    enmax_palette = ["#808282", "#C2CD23", "#918BC3"]
    color_codes_wanted = ['grey', 'green', 'purple']
    
    # Set the palette
    sns.set_palette(palette=enmax_palette)
    
    # Assign simple color codes to the palette
    

    Please help me to assign simple names to the colors using my "color_codes_wanted" list.