python制作气温分布图_Python案例:查询城市天气并绘制最高气温与最低气温的折线图...
1、编写源代码 - 查询城市天气.
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
# 功能:查询城市天气
import requests, json, re
from matplotlib import pyplot as plt
# 获取城市代码
def getCityCode(city):
url = 'http://toy1.weather.com.cn/search?cityname=' + city
r = requests.get(url)
if len(r.text) > 4:
json_arr = json.loads(r.text[1:len(r.text)-1])
code = json_arr[0]['ref'][0:9]
return code
else:
return "000000000"
# 获取城市天气信息
def getWeatherInfo(city):
code = getCityCode(city)
url = 'http://t.weather.sojson.com/api/weather/city/' + code
r = requests.get(url)
info = r.json()
weather = {}
if info['status'] == 200:
weather['城市:'] = info['cityInfo']['parent'] + info['cityInfo']['city']
weather['时间:'] = info['time'] + ' ' + info['data']['forecast'][0]['week']
weather['温度:'] = info['data']['forecast'][0]['high'] + ' ' + info['data']['forecast'][0]['low']
weather['天气:'] = info['data']['forecast'][0]['type']
else:
weather['错误:'] = '[' + city + ']不存在!'
return weather
# 打印天气信息
def printWeatherInfo(weather):
for key in weather:
print(key + weather[key])
# 获取未来气温
def getTemperatures(city):
code = getCityCode(city)
url = 'http://t.weather.sojson.com/api/weather/city/' + code
r = requests.get(url)
info = r.json()
temperatures = {}
if info['status'] == 200:
forecast = info['data']['forecast']
for i in range(len(forecast)):
dayinfo = forecast[i]
high = int(re.findall(r'\d+', dayinfo['high'])[0])
low = int(re.findall(r'\d+', dayinfo['low'])[0])
temperatures[dayinfo['ymd']] = [high, low]
else:
temperatures['错误:'] = '[' + city + ']不存在!'
return temperatures
# 打印未来气温
def printTemperatures(temperatures):
if '错误:' not in temperatures.keys():
for key in temperatures:
print(key + ' 高温:'+ str(temperatures[key][0]) + ' 低温:' + str(temperatures[key][1]))
# 绘制未来气温折线图
def drawTemperatureLineChart():
temperatures = getTemperatures(city)
if '错误:' not in temperatures.keys():
dates = []
highs = []
lows = []
for key in temperatures:
dates.append(key)
highs.append(temperatures[key][0])
lows.append(temperatures[key][1])
fig = plt.figure(dpi=81, figsize=(5,4))
plt.xlabel('Date (YYYY-MM-DD)', fontsize = 10)
plt.ylabel("Temperature (℃)", fontsize=10)
fig.autofmt_xdate()
plt.plot(dates, highs, c='red', alpha=0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)
city = input('输入城市名:')
printWeatherInfo(getWeatherInfo(city))
printTemperatures(getTemperatures(city))
drawTemperatureLineChart()
运行结果:
python制作气温分布图_Python案例:查询城市天气并绘制最高气温与最低气温的折线图...相关推荐
- Python案例:查询城市天气并绘制最高气温与最低气温折线图
Python案例:查询城市天气并绘制最高气温与最低气温折线图 一.解决思路 比如要查询"泸州"的天气. 1.首先获取泸州的城市代码 http://toy1.weather.com. ...
- javaScript学习笔记:用城市代码查询城市天气
javaScript学习笔记:AXAJ基础 一.准备工作 1.python案列:查询城市天气并绘制最高气温与最低气温折线图 2.使用JSON Viever直观查看JSON数据 二.任务 任务2.输入城 ...
- Java Web应用小案例:查询城市天气信息
Java Web应用小案例:查询城市天气信息 本期上大数据1班动态网站设计与开发课,经过半期的学习,学生已经可以利用所学的JSP知识开发简单的基于后台数据库操作的动态网站,但是这是远远不够的,课程教学 ...
- php显示出温度最好的城市,PHP微信开发之查询城市天气
PHP微信查询城市天气,首先,你需要找到一个获取天气的API,此处,我用的是百度的apistore,申请和使用API的网址:http://apistore.baidu.com/apiworks/ser ...
- python使用openweathermap API获取全世界主要城市天气信息
python使用openweathermap API获取全世界主要城市天气信息 官方网址: http://www.openweathermap.org/api 注意:使用gmail注册成功,然后到gm ...
- python的matplotlib库绘制条形图、散点图、饼图、折线图
python的matplotlib库绘制条形图.散点图.饼图.折线图 当我们学会了爬虫,抓取到了一些数据,接下来就是做数据分析了.本文章介绍绘制图形的基本代码. 打开cmd用pip 安装,若输入pip ...
- 输入第一行是一个整数n,代表小明做记录的天数 第二行是n个实数,代表每天早上的气温 输出 一个实数(保留1位小数),表示最低气温
问题 Z: 天气 时间限制: 1 Sec 内存限制: 128 MB 提交: 1272 解决: 935 [提交] [状态] [讨论版] [命题人:admin] 题目描述 小明是个南方人,感觉北方的冬天特 ...
- python制作英语字典_python如何制作英文字典
本文实例为大家分享了python制作英文字典的具体代码,供大家参考,具体内容如下 功能有添加单词,多次添加单词的意思,查询,退出,建立单词文件. keys=[] dic={} def rdic(): ...
- python获取数据库列名_python sqlite3 查询操作及获取对应查询结果的列名
记录查询操作及获取查询结果列字段的方法 1.sqlite3 中获取所有表名及各表字段名的操作方法 SQLite 数据库中有一个特殊的表叫 sqlite_master,sqlite_master 的结构 ...
最新文章
- 贝壳大数据OLAP平台架构演进
- python之multiprocessing创建进程
- linux 开机启动 自启动 设置
- 方舟服务器维护驯龙,方舟生存进化新手图文攻略 最详细的驯龙教程方法
- Pywinauto在Windows Twain Driver自动化测试中的应用研究
- HDU2897 邂逅明下
- linux操作系统启动盘,轻松制作Linux操作系统启动盘的四种通行方法
- 【书评】《技术垄断:文明向技术投降》
- 洛谷 P1192 台阶问题
- 安卓原生运行Win11 再跑 Apk,搁着套娃呢!
- vivo S7e和华为nova8se 的区别 哪个好
- U盘文件莫名丢失?这样做可轻松找回!
- 计算机与应用数学专业就业方向,2020数学与应用数学专业就业前景和就业方向分析...
- 微信小程序开发教程(破解版IDE 无内测资格也可使用)
- 如何对cnki知网上的论文进行可视化文本分析
- tga缩略图预览_带有缩略图预览的弹性图像幻灯片
- 搞一下AP AUTOSAR应用 | A1 从SOA-RM 到 SOA 到 AP AUTOSAR 应用
- 如何在Windows计算机上安装和使用Apple iCloud
- 60 个程序员才懂的梗!太形象了!
- 软件生命周期阶段有几个?常见软件生命周期模型有哪些?
热门文章
- 普罗米修斯 软件_Prometheus普罗米修斯
- ubuntu 安装微软雅黑和 Consolas 字体
- [AH2017/HNOI2017] 影魔
- r语言平均值显著性检验_R语言入门:检验相关性的显著性
- Castor XML
- 邢台职业技术学院计算机学费,邢台职业技术学院各专业学费标准
- Mac 使用教程之 PTGui 的下载与安装
- java练习:1. 求100以内的奇数和与偶数和
- 【java】java: -source 1.5 中不支持 diamond 运算符
- Centos无网环境下安装mysql步骤