简体   繁体   English

使用十六进制代码设置自定义的Seaborn调色板,并命名颜色

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

My company has a formal color palette so I need to use these colors in my seaborn charts. 我公司有一个正式的调色板,所以我需要在我的seaborn图表中使用这些颜色。 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. 因此,我想设置默认的seaborn调色板,并为这些颜色提供易于使用的名称,例如“ p”代表紫色,“ g”代表绿色。

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. 请帮助我使用“ color_codes_wanted”列表为颜色指定简单的名称。

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. 使用C {n}表示法。

It should be noted that all colors in seaborn are matplotlib colors. 应该注意的是,seaborn中的所有颜色都是matplotlib颜色。 One option matplotlib provides is the so called C{n} notation (with n = 0..9). matplotlib提供的一个选项是所谓的C {n}表示法(n = 0..9)。 By specifying a string like "C1" you tell matplotlib to use the second color from the current color cycle. 通过指定类似“ C1”的字符串,您告诉matplotlib使用当前颜色循环中的第二种颜色。 sns.set_palette sets the color cycle to your custom colors. sns.set_palette将颜色循环设置为自定义颜色。 So if you can remember their order in the cycle you can use this information and just specify "C1" for the second color. 因此,如果您可以记住它们在循环中的顺序,则可以使用此信息,只需为第二种颜色指定"C1"

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. 操作matplotlib颜色字典。

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: 此处显示的所有代码将以“公司的绿色”颜色产生相同的图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM