如何在Matplotlib中的堆叠水平条形图中显示数据值 [英] How to display data values in stacked horizontal bar chart in Matplotlib

查看:723
本文介绍了如何在Matplotlib中的堆叠水平条形图中显示数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我目前正在使用数据框绘制堆叠的水平条形图。代码如下

  new_data.plot.barh(stacked = True)

我得到如下图。





理想情况下,我想在其中显示数据值,如下所示。



我该如何实现?任何帮助表示赞赏。谢谢

解决方案

还有一个类似的问题


Hi I am currently plotting stacked horizontal bar chart using dataframe. The code is as below

new_data.plot.barh(stacked = True)

I get a chart like below.

Ideally I would want to have the data values displayed inside it, like below.

How do I accomplish this? Any help is appreciated. Thanks

解决方案

there's a similar question here, just use ax.text and adjust the x and y positioning according to your bar value and bar enumeration, for example:

import pandas as pd
df = pd.DataFrame({'value1':[10, 30, 20],'value2':[20,50,10]})
ax = df.plot.barh(stacked = True);
print(df)
for rowNum,row in df.iterrows():
    xpos = 0
    for val in row:
        xpos += val
        ax.text(xpos + 1, rowNum-0.05, str(val), color='black')
    xpos = 0
display(ax)

这篇关于如何在Matplotlib中的堆叠水平条形图中显示数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆