如何在matplotlib的条形图中的栏上显示文本? [英] How can I display text over columns in a bar chart in matplotlib?

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

问题描述

我有一个条形图,我想在每一列上显示一些文本,我该怎么做?

I have a bar chart and I want over each column to display some text,how can I do that ?

推荐答案

我相信这会为您指明正确的方向:

I believe this will point you in the right direction:

http://matplotlib.sourceforge.net/examples/pylab_examples/barchart_demo.html .

您最感兴趣的部分是:

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

文本的位置由height函数或列的高度确定,并且每列顶部的数字由以下字符写:'%d'%int(height).因此,您所需要做的就是创建一个字符串数组,称为名称",您要在列的顶部进行迭代.确保将格式更改为字符串(%s)而不是双精度.

The placement of the text is determined by the height function, or the height of the column, and the number that is put on top of each column is written by: '%d' %int(height). So all you need to do is create an array of strings, called 'name', that you want at the top of the columns and iterate through. Be sure to change the format to be for a string (%s) and not a double.

def autolabel(rects):
# attach some text labels
    for ii,rect in enumerate(rects):
        height = rect.get_height()
        plt.text(rect.get_x()+rect.get_width()/2., 1.02*height, '%s'% (name[ii]),
                ha='center', va='bottom')
autolabel(rects1)

应该这样做!

这篇关于如何在matplotlib的条形图中的栏上显示文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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