导入必要的第三方库

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. 学号20175313 《实现Linux下od -tx -tc XXX的功能》第九周

    目录 MyOD 一.题目要求 二.题目理解 三.需求分析 四.设计思路 五.代码链接 六.代码实现过程中遇到的问题 七.运行结果截图 八.参考资料 MyOD 一.题目要求 编写MyOD.java 用j ...

  2. jQuery通过ajax请求php遍历json数组到table中的代码

    html代码(test.html),js在html底部 具体代码如下所示: <!DOCTYPE html> <html lang="en"> <hea ...

  3. jenkins centos slave起不来报错The SSH key presented by the remote host does not match the key saved in the Known Hosts file against this host. Connections to this host will be denied until the two keys mat

    场景:我的centos-204是一台centos的机器,本来用https://www.cnblogs.com/zndxall/p/8297356.html 的centos slave方式搭建ok的,一 ...

  4. MySQL数据库常用命令和概念 (1)

    一.数据库的创建: 1.创建一个名称为mydb1的数据库 create database mydb1; 2.创建一个使用utf8字符集的mydb2数据库. create database mydb2 ...

  5. react github项目

    https://github.com/bailicangdu/react-pxq 网址;https://github.com/bailicangdu/react-pxq

  6. Splunk 简单笔记

    Splunk Notes source="c:\logs\abc.log" | rex field=url "(?<=\/)(?<ApiId>\w+?) ...

  7. 解决页面使用ifrmae后,在session失效后登录页面在子页面中显示(子窗体出现父窗体)

    在登录页面中添加js判断当前页面是否是父页面,诺不是则父页面跳转至登录页面. <script type="text/javascript"> //解决登录后多个父窗体问 ...

  8. codeforces-3

    C Game"23" #include<iostream> using namespace std; int main() { long long n,m; long ...

  9. 洛谷P2611 信息传递

    并查集裸题,记录每个点的胜读,取个min就好了 #include<stdio.h> #include<string.h> #include<algorithm> u ...

  10. mysql:数据库备份方案

    1. 数据库备份方案     1)没备份,跑路~     2)全量备份+增量备份 如果不小心“删库”,可以这么恢复: a. 将最近一次全量备份的全库找到,拷贝回来(文件一般比较大),解压,应用: b. ...