我有一个堆叠的条形图,但我想把它颠倒过来,把最小的条形放在上面。下面是我的代码:
import pandas as pd
data = {'fields': ['LastName', 'Race', 'Email', 'MidName', 'StreetAddress', 'Phone', 'State', 'FirstName', 'Gender', 'Expiration', 'StreetAddress'],
'Status': ['complete', 'missing', 'missing', 'complete', 'complete', 'missing', 'missing', 'complete', 'complete', 'complete', 'missing'],
'count': [27399, 146, 24225, 26078, 27269, 59, 28, 27399, 27403, 14017, 134]}
df = pd.DataFrame(data)
dfp = df.pivot(index='Status', columns='fields', values='count')
tot = dfp.sum(axis=1)
per = dfp.div(tot, axis=0).mul(100).round(2)
ax = per.plot(kind='bar', stacked=True, figsize=(8, 6), rot=0)
ax.legend(bbox_to_anchor=(1, 0.5), loc='center left', frameon=False)
ax = per.plot(kind='bar', stacked=True, figsize=(8, 6), rot=0)
for p in ax.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax.text(x+width/2,
y+height/2,
'{:.0f} %'.format(height),
horizontalalignment='center',
verticalalignment='center')
ax.legend(bbox_to_anchor=(1, 0.5), loc='center left', frameon=False)
字符串
1条答案
按热度按时间sg3maiej1#
我所要做的就是用[::-1]颠倒列的顺序,它工作得很好。它从下到上升序排序。我应该在我的原始代码中指定列按百分比
.df[df.columns[::-1]]
升序排列。x1c 0d1x的数据