利用天气API和pyecharts实现天气地图可视化

利用天气API和pyecharts实现天气地图可视化_第1张图片

#! /usr/bin/env/python
# -*- coding:UTF-8 -*-
# Author: Zhu Huaren

import requests
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import ChartType, SymbolType
import pandas as pd

key='天气API的key'
cities = ['北京','上海','天津','重庆','哈尔滨','齐齐哈尔','长春','吉林',
          '大连','沈阳','呼和浩特','鄂尔多斯','承德','石家庄','乌鲁木齐',
          '阿勒泰','皋兰','兰州','西宁','大通','西安','咸阳','银川','郑州',
          '开封','济南','青岛','太原','大同','合肥','阜阳','武汉','武昌',
          '长沙','浏阳','南京','无锡','成都','巴中','贵阳','六盘水','昆明',
          '官渡','南宁','兴宁','拉萨','杭州','苏州','南昌','九江','广州',
          '仁化','福州','泉州','台北','高雄','海口','三亚','香港','澳门',]
city_names = []
feel_likes = []
for city in cities:
    data_form = {
        'location':city,
        'key':key,
    }
    # 通过API接口获取天气数据
    get_data = requests.get(url='https://free-api.heweather.net/s6/weather/now',params=data_form).json()
    print(get_data)
    city_name = get_data['HeWeather6'][0]['basic']['location']
    feel_like = get_data['HeWeather6'][0]['now']['fl']
    data_pair = list(zip(city_name,feel_like))
    # print(data_pair)
    city_names.append(city_name)
    feel_likes.append(feel_like)
	# 构建图形
    c =Geo()
    # 提取各个城市的经纬度
    Latitude = get_data['HeWeather6'][0]['basic']['lat']
    Longitude = get_data['HeWeather6'][0]['basic']['lon']
    # 利用经纬度确定在地图上的位置
    c.add_coordinate(city_name,Longitude,Latitude)
    c.add_schema(maptype="china",itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"),)
    c.add("全国主要城市体感温度分布", [list(z) for z in zip(city_names,feel_likes)],type_=ChartType.EFFECT_SCATTER,)
    c.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    c.set_global_opts(
        visualmap_opts=opts.VisualMapOpts(min_=0,max_=40),
        title_opts=opts.TitleOpts(title="{}全国主要城市气温分布".format(get_data['HeWeather6'][0]['update']['loc'])),
        legend_opts=opts.LegendOpts(is_show=True)
    )
# 生成HTML文件
c.render('temperature.html')

你可能感兴趣的:(利用天气API和pyecharts实现天气地图可视化)