matplotlib – 基于色调/调色板的条形图中的边缘颜色

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

edgecolor参数不是为每个单独的条具有单独的颜色,而是将颜色应用于整个色调/组.

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

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

因此t_df将是,

《matplotlib – 基于色调/调色板的条形图中的边缘颜色》

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.

这给出的输出,

《matplotlib – 基于色调/调色板的条形图中的边缘颜色》

我想要的是蓝色条应该是蓝色边缘颜色和红色相同.这需要什么代码更改?

最佳答案 这有些苛刻,但它完成了工作:

《matplotlib – 基于色调/调色板的条形图中的边缘颜色》

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)
点赞