简体   繁体   English

在堆叠的条形图上适当放置注释

[英]Appropriately positioning annotations on stacked barplot

I have annotated each bar on my stacked barplot but can't seem to get the annotations to be equivalent to the bar's position. 我已经在堆叠的条形图上注释了每个条形,但是似乎无法使注释等​​于条形的位置。

This is the code I have: 这是我的代码:

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

This is what I am getting 这就是我得到的 在此处输入图片说明

I guess your main problem was that you placed the text in y direction effectively at 1.1 * i.get_height() , without considering the initial offset i.get_y() . 我想您的主要问题是您将文本有效地沿y方向放置在1.1 * i.get_height() ,而不考虑初始偏移量i.get_y()

Try this: 尝试这个:

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

Note that you may need to "play around" with good offsets, especially in y-direction. 请注意,您可能需要“偏移”具有良好的偏移量,尤其是在y方向上。 The ha="center",va="center" parameters align the text exactly at the chosen coordinate (both horizontally: ha and vertically: va), which comes in handy if you'd like to put the labels eg aligned below the top end of the patch: ha="center",va="center"参数精确地将文本与所选坐标对齐(水平:ha和垂直:va),如果您想将标签放置在顶部下方,例如,这会派上用场补丁的结尾:

ax.annotate(str(height),(ix+0.5*width, iy+1.0*height),ha="center",va="top")

Or just above the top end of the patch: 或正好位于补丁程序的顶端上方:

ax.annotate(str(height),(ix+0.5*width, iy+1.0*height),ha="center",va="bottom")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM