1 回答

TA貢獻1808條經驗 獲得超4個贊
我想您的主要問題是您將文本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")
添加回答
舉報