import matplotlib.pyplot as plt
import numpy as np
n = 12
X = np.arange(n)
Y1 = (1-X/n)*np.random.uniform(0.5,1.0,n)
Y1 = (1-X/n)*np.random.uniform(0.5,1.0,n)
# 由于返回值,进过提取是str,操作小数位数不方便,外面提前处理好
p1 = plt.bar(X,np.round(Y1,2),width=0.8,facecolor='deeppink',label='uniform')
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
plt.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
# 为什么有两个hight
def add_labels(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width()/2,height,height, ha='center', va='bottom')
rect.set_edgecolor('white')
# add_labels(p1)
autolabel(p1)
plt.legend(loc='best')
plt.show()
参考资料:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/102981.html