是否可以在seaborn.barplot中的每个单独的栏上添加阴影? [英] Is it possible to add hatches to each individual bar in seaborn.barplot?

查看:212
本文介绍了是否可以在seaborn.barplot中的每个单独的栏上添加阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ax = sns.barplot(x="size", y="algorithm", hue="ordering", data=df2, palette=sns.color_palette("cubehelix", 4))

在创建海洋条形图之后(或之前),我是否有办法传递每个条形图案的填充(图案和颜色一起填充)值? 在seabornmatplotlib中执行此操作的方法会很有帮助!

After (or before) creating a seaborn barplot, is there a way for me to pass in hatch (fill in patterns along with the colors) values for each bar? A way to do this in seaborn or matplotlib would help a lot!

推荐答案

您可以遍历通过捕获barplot返回的AxesSubplot,然后遍历其patches而创建的小节.然后,您可以使用.set_hatch()

You can loop over the bars created by catching the AxesSubplot returned by barplot, then looping over its patches. You can then set hatches for each individual bar using .set_hatch()

这是一个最小的示例,它是

Here's a minimal example, which is a modified version of the barplot example from here.

import matplotlib.pyplot as plt
import seaborn as sns

# Set style
sns.set(style="whitegrid", color_codes=True)

# Load some sample data
titanic = sns.load_dataset("titanic")

# Make the barplot
bar = sns.barplot(x="sex", y="survived", hue="class", data=titanic);

# Define some hatches
hatches = ['-', '+', 'x', '\\', '*', 'o']

# Loop over the bars
for i,thisbar in enumerate(bar.patches):
    # Set a different hatch for each bar
    thisbar.set_hatch(hatches[i])

plt.show()

感谢@kxirog在评论中提供此附加信息:

Thanks to @kxirog in the comments for this additional info:

for i,thisbar in enumerate(bar.patches)将从左到右一次遍历每种颜色,因此它将遍历左侧的蓝色条,然后是右侧的蓝色条,然后是左侧的绿色条,等等.

for i,thisbar in enumerate(bar.patches) will iterate over each colour at a time from left to right, so it will iterate over the left blue bar, then the right blue bar, then the left green bar, etc.

这篇关于是否可以在seaborn.barplot中的每个单独的栏上添加阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆