python | 画个简单的柱形图

python | 画个简单的柱形图,第1张

python | 画个简单的柱形图

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

参考资料:

python画图_大草的博客-CSDN博客_python 画图

 常见的python画图简单代码 - 知乎 (zhihu.com)

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5680511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存