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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值