Python从网络接口爬取json天气预报数据绘制高温低温双折线图

版权声明:本文为Zhang Phil原创文章,请不要转载! https://blog.csdn.net/zhangphil/article/details/88538112

Python从网络接口爬取json天气预报数据绘制高温低温双折线图

实现一个功能,从网络上的天气预报接口读取天气预报中的json数据,json数据中包含高温和低温以及日期,然后把日期作为横坐标,高温和低温分别做两条折线绘制在同一张图上。
第一步,需要从网路接口中读取json天气预报数据并解析json中包含的高温、低温数据以及对应的日期。本例中的天气预报json数据接口:http://t.weather.sojson.com/api/weather/city/101270101
读出来的json数据格式如下所示:

{
	"time": "2019-03-13 13:13:28",
	"cityInfo": {
		"city": "成都市",
		"cityId": "101270101",
		"parent": "四川",
		"updateTime": "12:39"
	},
	"date": "20190313",
	"message": "Success !",
	"status": 200,
	"data": {
		"shidu": "64%",
		"pm25": 83.0,
		"pm10": 139.0,
		"quality": "轻度污染",
		"wendu": "14",
		"ganmao": "儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼",
		"yesterday": {
			"date": "12",
			"sunrise": "07:20",
			"high": "高温 17.0℃",
			"low": "低温 11.0℃",
			"sunset": "19:09",
			"aqi": 114.0,
			"ymd": "2019-03-12",
			"week": "星期二",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "阵雨",
			"notice": "阵雨来袭,出门记得带伞"
		},
		"forecast": [{
			"date": "13",
			"sunrise": "07:19",
			"high": "高温 17.0℃",
			"low": "低温 11.0℃",
			"sunset": "19:10",
			"aqi": 110.0,
			"ymd": "2019-03-13",
			"week": "星期三",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "阵雨",
			"notice": "阵雨来袭,出门记得带伞"
		}, {
			"date": "14",
			"sunrise": "07:17",
			"high": "高温 18.0℃",
			"low": "低温 11.0℃",
			"sunset": "19:11",
			"aqi": 58.0,
			"ymd": "2019-03-14",
			"week": "星期四",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "15",
			"sunrise": "07:16",
			"high": "高温 14.0℃",
			"low": "低温 9.0℃",
			"sunset": "19:11",
			"aqi": 66.0,
			"ymd": "2019-03-15",
			"week": "星期五",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "16",
			"sunrise": "07:15",
			"high": "高温 12.0℃",
			"low": "低温 10.0℃",
			"sunset": "19:12",
			"aqi": 52.0,
			"ymd": "2019-03-16",
			"week": "星期六",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "17",
			"sunrise": "07:14",
			"high": "高温 14.0℃",
			"low": "低温 10.0℃",
			"sunset": "19:13",
			"aqi": 65.0,
			"ymd": "2019-03-17",
			"week": "星期日",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "18",
			"sunrise": "07:13",
			"high": "高温 15.0℃",
			"low": "低温 11.0℃",
			"sunset": "19:13",
			"aqi": 69.0,
			"ymd": "2019-03-18",
			"week": "星期一",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "19",
			"sunrise": "07:11",
			"high": "高温 14.0℃",
			"low": "低温 9.0℃",
			"sunset": "19:14",
			"ymd": "2019-03-19",
			"week": "星期二",
			"fx": "无持续风向",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "20",
			"sunrise": "07:10",
			"high": "高温 17.0℃",
			"low": "低温 6.0℃",
			"sunset": "19:15",
			"ymd": "2019-03-20",
			"week": "星期三",
			"fx": "东北风",
			"fl": "<3级",
			"type": "阴",
			"notice": "不要被阴云遮挡住好心情"
		}, {
			"date": "21",
			"sunrise": "07:09",
			"high": "高温 17.0℃",
			"low": "低温 7.0℃",
			"sunset": "19:15",
			"ymd": "2019-03-21",
			"week": "星期四",
			"fx": "东南风",
			"fl": "<3级",
			"type": "阴",
			"notice": "不要被阴云遮挡住好心情"
		}, {
			"date": "22",
			"sunrise": "07:08",
			"high": "高温 18.0℃",
			"low": "低温 10.0℃",
			"sunset": "19:16",
			"ymd": "2019-03-22",
			"week": "星期五",
			"fx": "东风",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "23",
			"sunrise": "07:06",
			"high": "高温 21.0℃",
			"low": "低温 11.0℃",
			"sunset": "19:16",
			"ymd": "2019-03-23",
			"week": "星期六",
			"fx": "东南风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "24",
			"sunrise": "07:05",
			"high": "高温 19.0℃",
			"low": "低温 12.0℃",
			"sunset": "19:17",
			"ymd": "2019-03-24",
			"week": "星期日",
			"fx": "东北风",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "25",
			"sunrise": "07:04",
			"high": "高温 18.0℃",
			"low": "低温 12.0℃",
			"sunset": "19:18",
			"ymd": "2019-03-25",
			"week": "星期一",
			"fx": "东北风",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "26",
			"sunrise": "07:03",
			"high": "高温 20.0℃",
			"low": "低温 12.0℃",
			"sunset": "19:18",
			"ymd": "2019-03-26",
			"week": "星期二",
			"fx": "东风",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "27",
			"sunrise": "07:02",
			"high": "高温 20.0℃",
			"low": "低温 13.0℃",
			"sunset": "19:19",
			"ymd": "2019-03-27",
			"week": "星期三",
			"fx": "东南风",
			"fl": "<3级",
			"type": "阴",
			"notice": "不要被阴云遮挡住好心情"
		}]
	}
}


第二步,高温,低温,日期数据具备后,开始绘制折线图。
程序代码如下所示:

import json
import urllib.request
import matplotlib
from matplotlib import pyplot as plt
from datetime import datetime


def get_weather_json_data():
    #101270101,天气预报中成都的代码。
    url = "http://t.weather.sojson.com/api/weather/city/101270101"
    response = urllib.request.urlopen(url)
    content = response.read().decode('utf-8')
    return content


def get_forecast_data(content):
    data = content["data"]
    return data["forecast"]


#清洗数据。过滤℃
def get_pure_temperature(temp):
    a, b = temp.split()
    return b.strip().strip("℃")


def make_chart(high, low, date_time):
    matplotlib.rc('font', family='SimHei', weight='bold')
    plt.rcParams['axes.unicode_minus'] = False

    x = range(len(date_time))

    plt.plot(x, low, ms=10,marker='*', color='blue', alpha=0.5, label="低温")
    plt.plot(x, high, ms=10,marker='o', color='red', alpha=0.5, label="高温")

    plt.fill_between(x, low, high, facecolor='gray', alpha=0.1)
    plt.title("2019年3月 - 温度变化", fontsize=15)

    plt.xticks(x, date_time, rotation=20)
    
    plt.xlabel('日期')
    plt.ylabel('温度')
    plt.grid()  # 显示网格
    plt.legend()
    plt.show()


content = json.loads(get_weather_json_data())
data = get_forecast_data(content)

high, low, date_time = [], [], []
for obj in data:
    h = obj["high"]
    high.append(get_pure_temperature(h))

    l = obj["low"]
    low.append(get_pure_temperature(l))

    date_time.append(obj["ymd"])



#取得高温低温和日期,开始绘折线图。
make_chart(high, low, date_time)


运行结果如图:

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/88538112
今日推荐