Spa*_*att 6 matplotlib seaborn
我试图设置barplot使用创建的边缘颜色seaborn。问题似乎是当我使用hue参数时。
代替具有的单独的颜色为每个单独的条,所述edgecolor参数应用于颜色与全色调/组。
通过此简单示例重现该问题。
tips = sns.load_dataset("tips")
t_df = tips.groupby(['day','sex'])['tip'].mean().reset_index()
Run Code Online (Sandbox Code Playgroud)
因此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.
Run Code Online (Sandbox Code Playgroud)
这给出的输出,
我想要的是蓝色条应该具有蓝色边缘颜色,红色也应如此。这需要什么代码更改?
这有点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)
Run Code Online (Sandbox Code Playgroud)
@mwaskom 的建议显然要简洁得多。为了完整性:
for patch in t_ax.patches:
clr = patch.get_facecolor()
patch.set_edgecolor(clr)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
477 次 |
| 最近记录: |