条形图中的边缘颜色基于 hue/palette

Edge colors in barplots based on hue/palette

我正在尝试为使用 seaborn 创建的 barplot 设置边缘颜色。问题似乎是当我使用 hue 参数时。

edgecolor 参数将颜色应用到整个 hue/group。

,而不是为每个单独的条使用 单独的颜色

通过这个简单的例子重现问题。

tips = sns.load_dataset("tips")
t_df = tips.groupby(['day','sex'])['tip'].mean().reset_index()

因此 t_df 将是,

clrs = ["#348ABD", "#A60628"]
t_ax = sns.barplot(x='day',y='tip',hue='sex',data=t_df,alpha=0.75,palette= sns.color_palette(clrs),edgecolor=clrs)
plt.setp(t_ax.patches, linewidth=3)   # This is just to visualize the issue.

这给出的输出,

我想要的是蓝色条应该有蓝色边缘颜色,红色也一样。这需要更改什么代码?

这有点 hacky,但它完成了工作:

import matplotlib.patches

# grab everything that is on the axis
children = t_ax.get_children()

# filter for rectangles
for child in children:
    if isinstance(child, matplotlib.patches.Rectangle):

        # match edgecolors to facecolors
        clr = child.get_facecolor()
        child.set_edgecolor(clr)

编辑:

@mwaskom 的建议显然更简洁。为了完整起见:

for patch in t_ax.patches:
    clr = patch.get_facecolor()
    patch.set_edgecolor(clr)