Jac*_*ack 4 python matplotlib pandas
我的数据框如下所示:
Airport ATA Cost Destination Handling Custom Total Cost
0 PRG 599222 11095 20174 630491
1 LXU 364715 11598 11595 387908
2 AMS 401382 23562 16680 441623
3 PRG 599222 11095 20174 630491
Run Code Online (Sandbox Code Playgroud)
使用下面的代码,它给出了堆积的条形图:
df = df.iloc[:, 0:4]
df.plot(x='Airport', kind='barh', stacked=True, title='Breakdown of Costs', mark_right=True)
Run Code Online (Sandbox Code Playgroud)
如何在每个堆叠的条形图上添加总计(以千分之千为单位)?如何为%堆叠条形图中的每个细分添加?
Spg*_*tCd 10
您可以plt.text根据信息将信息放置在各个位置。
但是,如果条形很小,则可能需要进行一些调整才能看起来完美。
df_total = df['Total Cost']
df = df.iloc[:, 0:4]
df.plot(x = 'Airport', kind='barh',stacked = True, title = 'Breakdown of Costs', mark_right = True)
df_rel = df[df.columns[1:]].div(df_total, 0)*100
for n in df_rel:
for i, (cs, ab, pc, tot) in enumerate(zip(df.iloc[:, 1:].cumsum(1)[n], df[n], df_rel[n], df_total)):
plt.text(tot, i, str(tot), va='center')
plt.text(cs - ab/2, i, str(np.round(pc, 1)) + '%', va='center', ha='center')
Run Code Online (Sandbox Code Playgroud)
编辑:一些更好的可读性的主意:
将总值向右移动,使用45°旋转文本:
plt.text(tot+10000, i, str(tot), va='center')
plt.text(cs - ab/2, i, str(np.round(pc, 1)) + '%', va='center', ha='center', rotation=45)
Run Code Online (Sandbox Code Playgroud)
在顶部和底部对齐的文本之间切换:
va = ['top', 'bottom']
va_idx = 0
for n in df_rel:
va_idx = 1 - va_idx
for i, (cs, ab, pc, tot) in enumerate(zip(df.iloc[:, 1:].cumsum(1)[n], df[n], df_rel[n], df_total)):
plt.text(tot+10000, i, str(tot), va='center')
plt.text(cs - ab/2, i, str(np.round(pc, 1)) + '%', va=va[va_idx], ha='center')
Run Code Online (Sandbox Code Playgroud)
仅标记10%或以上的条:
if pc >= 10:
plt.text(cs - ab/2, i, str(np.round(pc, 1)) + '%', va='center', ha='center')
Run Code Online (Sandbox Code Playgroud)
...或仍然打印它们,但垂直:
if pc >= 10:
plt.text(cs - ab/2, i, str(np.round(pc, 1)) + '%', va='center', ha='center')
else:
plt.text(cs - ab/2, i, str(np.round(pc, 1)) + '%', va='center', ha='center', rotation=90)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7297 次 |
| 最近记录: |