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

Python中matplotlib简单使用

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

Python中matplotlib简单使用

题目:绘制上海北京温度变化图,如图所示

y使用np.random.uniform()方法

 代码:

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif']='FangSong'       # 总体字体
plt.rcParams['axes.unicode_minus']=False

#获取数据
x=range(60)
y_shanghai=[np.random.uniform(25,30) for i in x]
y_beijin=[np.random.uniform(5,10) for i in x]

#处理数据:pandas,numpypandas 略
#特征工程:sklearn 略

#画布
fig,axe=plt.subplots(nrows=1,ncols=2,figsize=(20,8))

#绘图
axe[0].plot(x,y_shanghai,label='shanghai',linestyle='-.',color='r')
axe[1].plot(x,y_beijin,label='beijin',linestyle='--',color='y')

#辅助显示
x_ticks=['11点{}分'.format(i) for i in x]
y_ticks=['{}℃'.format(j) for j in range(0,40,5)]
axe[0].set_xticks(x[::10])
axe[0].set_xticklabels(x_ticks[::10])
axe[0].set_yticks(range(0,40,5))
axe[0].set_yticklabels(y_ticks)
axe[0].set_xlabel('时间',fontproperties="SimHei", fontsize=10)  # 局部字体
axe[0].set_ylabel('摄氏度')
axe[0].set_title('上海温度')
axe[1].set_xticks(x[::10])
axe[1].set_xticklabels(x_ticks[::10])
axe[1].set_yticks(range(0,40,5))
axe[1].set_yticklabels(y_ticks)
axe[1].set_xlabel('时间')
axe[1].set_ylabel('摄氏度')
axe[1].set_title('北京温度')


#显示
axe[0].legend(loc='upper left')
axe[1].legend(loc='upper right')
plt.savefig('上海北京最新天气.png')
plt.show()

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

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

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