【发布时间】:2019-05-09 15:26:01
【问题描述】:
我正在尝试使用自定义调色板将不同的值应用于 seaborn 中的 hue 类别,但输出颜色与我的输入不匹配。举个例子:
import random
random.seed(1)
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
BLUE = (0, 160/255, 240/255)
YELLOW = (1, 210/255, 25/255)
GREEN = (110/255, 200/255, 5/255)
sns.set_palette(
palette=[BLUE, YELLOW, GREEN], n_colors=3
)
sns.palplot(sns.color_palette())
plt.show()
df = pd.DataFrame(
{
'x': [0, 0, 0, 1, 1, 1, 2, 2, 2],
'y': [random.random() for _ in range(9)],
'hue': ['A', 'B', 'C'] * 3
}
)
sns.barplot(data=df, x='x', y='y', hue='hue')
plt.show()
palplot 按预期显示颜色:
但是,当它们在 barplot 中使用时,它们会静音:
我的猜测是seaborn 在背景颜色之间进行插值。有什么办法可以防止这种情况发生,只使用我定义的离散颜色?
【问题讨论】: