在堆积条形图上适当定位注释

我已经在堆叠的条形图上注释了每个条,但似乎无法使注释与条的位置相同。


这是我的代码:


for i in ax_mult.patches:

  width,height=i.get_width(),i.get_height()

  x,z =i.get_xy()

  ax_mult.annotate(str(i.get_height()),(i.get_x()+.30*width,i.get_height()+.1*height))

这就是我得到的

http://img3.mukewang.com/61dd475f000199aa06360223.jpg

至尊宝的传说
浏览 140回答 1
1回答

炎炎设计

我想您的主要问题是您将文本y有效地放置在方向上1.1 * i.get_height(),而没有考虑初始偏移量i.get_y()。试试这个:for i in ax_mult.patches:    ix,iy=i.get_x(),i.get_y() ## gives you the bottom left of each patch    width,height=i.get_width(),i.get_height() ## the width & height of each patch    ## to place the annotation at the center (0.5, 0.5):    ax.annotate(str(height),(ix+0.5*width, iy+0.5*height),ha="center",va="center")    ## alternatively via ax.text():    # ax.text(ix+.5*width,iy+.5*height,height,ha="center",va="center" ) 请注意,您可能需要使用良好的偏移“玩转”,尤其是在 y 方向。该ha="center",va="center"参数正好对准在所选择的坐标文本(同时在水平:HA和垂直:VA),其中就派上用场了,如果你想放的标签如补丁的顶端对齐如下:ax.annotate(str(height),(ix+0.5*width, iy+1.0*height),ha="center",va="top")或者就在补丁的顶端上方:ax.annotate(str(height),(ix+0.5*width, iy+1.0*height),ha="center",va="bottom")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python