栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python | 画个简单的柱形图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/675307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号