删除seaborn酒吧情节中的图例,但保留自定义颜色



我正试图基于df中的一列创建一个具有自定义颜色的条形图。不过,我不想显示图例。只要我尝试删除图例的颜色就回到默认值。

这是我的密码。

b = sns.barplot(x="game_week", y="shots", data=data, hue="color")
b.tick_params(labelsize=5)
b.legend_.remove()
b.set(ylim=(0, np.nanmax(df[cols])))

有什么想法吗?sns.barplot有可能吗?

您可以使用seaborn.color_palette设置调色板,如下所示:

palette = sns.color_palette('hls', len(data.color.unique())
ax = sns.barplot(
x="game_week", 
y="shots", 
data=data, 
hue="color",
palette=palette
)
ax.legend_.remove()

通过以这种方式设置调色板,您将获得与数据帧的color列中的类别数量相对应的多个颜色,这也是色盲友好

最新更新