【问题标题】:Edge colors in barplots based on hue/palette基于色调/调色板的条形图中的边缘颜色
【发布时间】:2017-05-11 20:12:29
【问题描述】:

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

edgecolor 参数不是为每个单独的条设置一个单独的颜色,而是将颜色应用于整个色调/组。

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

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.

这给出的输出,

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

【问题讨论】:

  • 你应该提交一份错误报告。

标签: matplotlib seaborn


【解决方案1】:

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

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)

【讨论】:

  • 您还可以在ax.patches 列表中更直接地访问酒吧。
  • @Paul 你描述的方式很有效。 @mwaskom - 所以sns.barplot 方法没有办法做到这一点?
  • 谢谢@mwaskom,我总是忘记你可以直接获取补丁。添加了您的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2021-12-29
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多