【发布时间】:2021-07-28 16:25:57
【问题描述】:
我想在 seaborn barplot 上添加重复的填充图案。
当然,我检查了下面的问答,但我无法解决我的问题。
Is it possible to add hatches to each individual bar in seaborn.barplot?
在上面的问答中,不同的补丁图案应用于左侧蓝色('-')和右侧蓝色('+')。 我想将相同的补丁图案应用于相同的颜色条; ("//") 应该应用在两个蓝色条上, ("\\") 应该应用在两个绿色条上, ("|") 应该应用在两个红色条上。
我尝试了下面的代码,但我无法实现我的理想身材。
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);
hatches = cycle([ "//" , "\\\\" , "|"])
# 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()
【问题讨论】:
标签: python-3.x seaborn