百度地图API获取最近五天天气并可视化展示

'''
天气预报小程序 V0.1
'''
import urllib.request
import json
import string
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties #字体管理器

def Weather_forcast(city,district_code):
    Font = FontProperties(fname="C:\Windows\Fonts\simkai.ttf")

    url = 'http://api.map.baidu.com/weather/v1/?district_id' \
          '='+str(district_code)+'&data_type=all&ak=u49Sd4WWCKnHv8aNwbmAyYl31mw8F18o'
    #print(url)
    url = urllib.parse.quote(url, safe=string.printable)
    page = urllib.request.urlopen(url)
    res = json.loads(page.read())  # json转为dict,json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型
    # print(res["result"]["location"])
    #print(res)

    y_max = []  # 最高温度
    y_min = []  # 最低温度
    x_date = []  # 日期

    for i in res['result']['forecasts']:
        y_max.append(i['high'])
        y_min.append(i['low'])
        x_date.append(i['date'])
    plt.plot(x_date, y_max, color="r", linestyle='--', marker='.')
    plt.plot(x_date, y_min, color='b', linestyle='-.', marker='*')
    # plt.yticks(np.arange(-8,17,4))
    plt.xlabel("日期", FontProperties=Font)
    plt.ylabel("℃")
    plt.title(city + " \n"
                     "uptime:" + str(res['result']['now']['uptime']), FontProperties=Font)
    plt.legend(["max", "min"])
    plt.show()

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值