[英]Removing legend from seaborn barplot but keeping custom color
I'm trying to create a barplot with custom colors based on one of the column in the df.我正在尝试根据 df 中的列之一创建带有自定义颜色的条形图。 However I don't want legend to display.
但是我不想显示图例。 As soon as I try to remove legend color is back to default.
一旦我尝试删除图例颜色,它就会恢复为默认值。
Here is my code.这是我的代码。
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])))
Any ideas?有任何想法吗? Is it even possible with sns.barplot?
sns.barplot 甚至有可能吗?
You can set the color palette with seaborn.color_palette like this:您可以使用seaborn.color_palette设置调色板,如下所示:
palette = sns.color_palette('hls', len(data.color.unique())
g = sns.barplot(
x="game_week",
y="shots",
data=data,
hue="color",
palette=palette
)
g.legend_.remove()
By setting the palette this way you will get a number of colors corresponding to the number of categories in the color
column of your dataframe that is also colorblind friendly !通过以这种方式设置调色板,您将获得与数据框
color
列中类别数量相对应的多种color
,这些color
也是色盲友好的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.