【问题标题】:Annotation of horizontal bar graphs in matplotlib [duplicate]matplotlib中水平条形图的注释[重复]
【发布时间】:2016-10-13 20:55:18
【问题描述】:

我需要注释水平条形图。 我可以使用 matplotlib 网站中显示的示例来注释垂直条形图,但类似的 Horizo​​natl 想法似乎不起作用。

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

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()

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

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()

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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:
    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')
    

    【讨论】:

      猜你喜欢
      • 2017-08-09
      • 2018-07-22
      • 2020-11-22
      • 2017-10-18
      • 1970-01-01
      • 2016-07-14
      • 2013-05-15
      • 2020-08-10
      • 1970-01-01
      相关资源
      最近更新 更多