使用自定义函数
如前所述,您可以创建一个函数,如果使用自定义colorname调用该函数,将从列表中返回十六进制颜色。
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()
使用C{n}表示法。
sns.set_palette
将颜色循环设置为自定义颜色。所以,如果你能记住它们在循环中的顺序,你可以使用这些信息,只需指定
"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()
所有命名颜色都存储在字典中,您可以通过
matplotlib.colors.get_named_colors_mapping()
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()
此处显示的所有代码将生成相同的“公司绿色”绘图: