[英]Plot a stacked bar plot in seaborn with hatching
I am trying to plot a stacked bar plot using seaborn/matplotlib with hatching.我正在尝试使用带孵化的 seaborn/matplotlib 绘制堆积条形图。 But the hatching is not proper.
但是孵化不合适。 Its coming as shown in image
它的到来如图所示
My code is as below:我的代码如下:
sc_bar=sns.barplot(x='Salt Concentration',y='EPS
Produced',data=df_salt_conc_mod,hue='Strain',fill=False,edgecolor='black')
bars = sc_bar.patches
pattern=['//','..','xx','*']
hatches=np.tile(pattern,7)
i=0
for bar in bars:
bar.set_hatch(pattern[i])
i+=1
count+=1
if(i>3):
i=0
sc_bar.legend()
What am i doing wrong?我究竟做错了什么?
Let's try zip
:让我们试试
zip
:
df = sns.load_dataset('tips')
sc_bar = sns.barplot(data=df, x='tip', y='sex', hue='day', fill=False)
bars = sc_bar.patches
pattern=['//','..','xx','*']
# replace 2 with 7 in your code
hatches=np.repeat(pattern,2)
for pat,bar in zip(hatches,bars):
bar.set_hatch(pat)
sc_bar.legend()
Output:输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.