# 函数功能:添加指向型注释文本
def arrow_label(rects, label):
# 找到最小值的索引
min_index = np.argmin([rect.get_height() for rect in rects])
min_rect = rects[min_index] # 获取最小值对应的矩形条对象
x = min_rect.get_x() + min_rect.get_width() / 2 # 获取矩形条的中心 x 坐标
y = min_rect.get_height() # 获取矩形条的高度
offset = y * 0.03 # 箭头和文本的偏移量,根据需要调整
plt.annotate(label, (x, y), xytext=(x, y + offset),
color='red', arrowprops=dict(arrowstyle="->", color='red'))
arrow_label(bar_rects, "最小值")
3.6
最新推荐文章于 2024-09-02 23:14:47 发布