matplotlib中的水平条形图注释 [英] Annotation of horizontal bar graphs in matplotlib

查看:97
本文介绍了matplotlib中的水平条形图注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要注释水平条形图. 我可以使用matplotlib网站上显示的示例注释垂直条形图,但对horizo​​ntal的类似想法似乎行不通.

I need to annotate horizontal bar graphs. I am able to annotate vertical bar graphs using the example shown in matplotlib website but a similar idea for horizonatl doesn't seem to working.

以下是垂直的小型工作示例

The following is small working example for vertical

from pylab import *

ops = 90*rand(4)    # the bar lengths
pos = arange(4)+.5    # the bar centers on the y axis
rects1 = bar(pos, ops)

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')
autolabel(rects1)

show()

以下是我想使用的代码,但不适用于水平图

The following is the code I want to get working but doesn't work for horizontal graphs

from pylab import *

ops = 90*rand(4)    # the bar lengths
pos = arange(4)+.5   
rects1 = barh(pos, ops)

def autolabel(rects):
    for rect in rects:
        width = rect.get_width()
        plt.text(rect.get_y() - 1.05*rect.get_height()/2., 1.00*width,
                '%d' % int(width),
                ha='center', va='bottom')
autolabel(rects1)

show()

任何帮助表示感谢,在此先感谢!

Any help appreciated, thanks in advance!

推荐答案

def autolabel(rects):
    for rect in rects:
        width = rect.get_width()
        plt.text(1.05*rect.get_width(), rect.get_y()+0.5*rect.get_height(),
                 '%d' % int(width),
                 ha='center', va='center')

这篇关于matplotlib中的水平条形图注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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