导入必要的第三方库

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

到此为止教程结束

拓展思考

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

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


Python绘制温度变化曲线的更多相关文章

  1. Python绘制正态分布曲线

      使用Python绘制正态分布曲线,借助matplotlib绘图工具: \[ f(x) = \dfrac{1}{\sqrt{2\pi}\sigma}\exp(-\dfrac{(x-\mu)^2}{2 ...

  2. Python绘制KS曲线

    更多大数据分析.建模等内容请关注公众号<bigdatamodeling> python实现KS曲线,相关使用方法请参考上篇博客-R语言实现KS曲线 代码如下: ############## ...

  3. 使用python和pygame绘制繁花曲线

    前段时间看了一期<最强大脑>,里面展示了各种繁花曲线组合成的非常美丽的图形,一时心血来潮,想尝试自己用代码绘制繁花曲线,想怎么组合就怎么组合. 真实的繁花曲线使用一种称为繁花曲线规的小玩意 ...

  4. ROC,AUC,PR,AP介绍及python绘制

    这里介绍一下如题所述的四个概念以及相应的使用python绘制曲线: 参考博客:http://kubicode.me/2016/09/19/Machine%20Learning/AUC-Calculat ...

  5. Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢?

    Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢? 可视化图表,有相当多种,但常见的也就下面几种,其他比较复杂一点,大都也是基于如下几种进行组合,变换出来的.对于初学者来说,很容易被这官网上 ...

  6. Python绘制3D图形

    来自:https://www.jb51.net/article/139349.htm 3D图形在数据分析.数据建模.图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行 ...

  7. 用html5的canvas画布绘制贝塞尔曲线

    查看效果:http://keleyi.com/keleyi/phtml/html5/7.htm 完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  8. Python绘制PDF文件~超简单的小程序

    Python绘制PDF文件 项目简介 这次项目很简单,本次项目课,代码不超过40行,主要是使用 urllib和reportlab模块,来生成一个pdf文件. reportlab官方文档 http:// ...

  9. Matlab 如何绘制复杂曲线的包络线

    Matlab 如何绘制复杂曲线的包络线 http://jingyan.baidu.com/article/aa6a2c14d36c710d4c19c4a8.html 如果一条曲线(比如声音波形)波动很 ...

随机推荐

  1. #20175201 实验一 Java开发环境的熟悉(Linux + Eclipse)

    一.实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. (一)命令行下Java程序开发 1.过程 2.结果 调试代码遇到的问题: 解决 ...

  2. 微信小程序wx.uploadFile 上传文件 的两个坑

    fileUpload: function (tempFilePath) { var that = this;//坑1: this需要这么处理 wx.uploadFile({ url: url地址, / ...

  3. BIML 101 - ETL数据清洗 系列 - BIML 快速入门教程 - 连接数据库执行SQL语句

    BIML 101 - BIML 快速入门教程 第一节 连接数据库执行SQL语句 本小节将用BIML建一个简单的可以执行的包. 新建一个biml文件,贴入下面的代码 1 <Biml xmlns=& ...

  4. mysql 获取昨天数据 utc时间

    # yzj邀请昨日数据 SELECT s.id, s.create_at, ch.id, ch.code AS channel, c.id , c.code AS custom, so.id, so. ...

  5. LeetCode 15 输入无序、有重复,输出排重版 3-Sum

    V1 粗暴的遍历,时间复杂度O(N³) func threeSumClosest(nums []int, target int) int { min := 0 result := 0 for i := ...

  6. IDEA 破解_补丁永久_2018.3

           主要是Eclipse我已经玩坏了三次了,切换jdk8到jdk10,再切换到jdk8,大量文件乱码,怎么改都没用,有的时候Eclipse久了不用,项目放在里面发霉了,坏掉了,MMP,换到I ...

  7. DotNetty网络通信框架学习

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

  8. Python魔法方法详解

      魔法方法 含义   基本的魔法方法 __new__(cls[, ...]) 1.__new__是在一个对象实例化的时候所调用的第一个方法 2.它的第一个参数是这个类,其他的参数是用来直接传递给__ ...

  9. python locust 性能测试:HttpSession

    官网解释:用于在请求之间执行Web请求和保留(会话)cookie的类(以便能够登录和退出网站):记录每个请求,以便locust可以显示统计信息: from locust import TaskSet, ...

  10. Connector for Python

    连接mysql, 需要mysql connector, conntector是一种驱动程序,python连接mysql的驱动程序,mysql官方给出的名称为connector/python, 可参考m ...