Python绘制温度变化曲线

导入必要的第三方库

from requests import get
import matplotlib.pyplot as plt
/usr/lib/python3/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/usr/lib/python3/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
from dateutil import parser
url = 'https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getallmeasurements/505307'
weather = get(url).json()
weather['items'][0]['ambient_temp']
9.77
weather['items'][0]
{'air_pressure': 982.51,
 'air_quality': 75.82,
 'ambient_temp': 9.77,
 'created_by': 'Test Brompton Academy',
 'created_on': '2016-11-19T22:50:00Z',
 'ground_temp': -1000,
 'humidity': 71.11,
 'id': 1707846,
 'rainfall': 0,
 'reading_timestamp': '2016-11-19T22:50:00Z',
 'updated_by': 'Test Brompton Academy',
 'updated_on': '2016-11-20T00:56:09.435Z',
 'weather_stn_id': 505307,
 'wind_direction': 111.92,
 'wind_gust_speed': 22.8,
 'wind_speed': 12.34}

迭代天气数据集,取出温度数据,并且保存到列表

利用for循环获取温度列表

temperatures = []
for record in weather['items']:
    temperature = record['ambient_temp']
    temperatures.append(temperature)

利用列表生成式来获取温度数据

temperatures = [record['ambient_temp'] for record in weather['items']]

把RaspberryPi的数据库所用的日期转换成Python中的datetime对象格式

parser.parse(weather['items'][0]['reading_timestamp'])
datetime.datetime(2016, 11, 19, 22, 50, tzinfo=tzutc())

用列表生成式来获取日期列表

timestamps = [parser.parse(record['reading_timestamp']) for record in weather['items']]

绘制图像

  1. 绘图需要的代码很少,首先声明绘图要用的数据集合,然后第二个用来展示数据:
## 绘制温度时间变化图
plt.plot(timestamps, temperatures)
plt.ylabel('温度')
plt.xlabel('时间')
plt.show()

122898-20170130190218573-334181937.png

到此为止教程结束

拓展思考

  1. 比如物理的加速度实验,用纸袋获取数据后可以用这种方法绘图
  2. 生物实验观测的数据绘图
  3. 数学图像,统计观察

如果是实际实验获得的数据,获取数据的过程可以大大简化,这样只需要8行左右的代码就可以绘制一些实验曲线

转载于:https://www.cnblogs.com/asworm/p/6358271.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值