如上图,为翻转的直方图,红色数字标注了最大值所在的区间和柱形图上。
主要还是要理解Rectangle的patch用法
# 作图
n,bins,patches = plt.hist(data, bins=20, density=True, facecolor='green', edgecolor='black', orientation='horizontal', alpha=0.7)
# 标注最大值
width = [i.get_width() for i in patches]
y = [i.get_y() for i in patches]
max_width = max(width)
index = width.index(max_width)
max_val = y[index]
plt.text(max_width,max_val,'%.2f'%max_val,ha='right',color='red')