在堆叠栏中显示百分比

时间:2020-03-20 15:44:52

标签: python matplotlib

我有2组数据需要将它们组合在一起。

data

我有以下代码来生成条形图:

table = pd.crosstab(data['Gender'],data['I have flirted with someone at work before'])
table.div(table.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True)
plt.title('Relationship between gender and flirting behaviour',fontweight='bold')
plt.xlabel('Gender')
plt.ylabel('Flirting behaviour')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
bars = ('Female', 'Male')
y_pos = np.arange(len(bars))
plt.xticks(y_pos,bars, color='black', rotation=0, fontweight='normal',
           fontsize='12', horizontalalignment='center')

它生成以下图: Plot 1

但是,我希望条形图中包含相应的百分比。 例如 : Plot 2

我该怎么做?

0 个答案:

没有答案