python中进行图表绘制的库主要有两个:matplotlib 和 pyecharts, 相比较而言:
matplotlib中提供了BaseMap可以用于地图的绘制,但是个人觉得其绘制的地图不太美观,而且安装相较而言有点麻烦。
pyecharts是基于百度开源的js库echarts而来,其最大的特点是:安装简单、使用也简单。
所以决定使用pyecharts来绘制地图。
1.安装pyecharts
如果有anaconda环境,可用 pip install pyecharts 命令安装pyecharts。
由于我们要绘制中国的疫情地图,所以还要额外下载几个地图。地图文件被分成了三个Python包,分别为:
全球国家地图: echarts-countries-pypkg
安装命令:pip install echarts-countries-pypkg
中国省级地图: echarts-china-provinces-pypkg
安装命令:pip install echarts-china-provinces-pypkg
中国市级地图: echarts-china-cities-pypkg
安装命令:pip install echarts-china-cities-pypkg
2.导包。
绘制地图时我们根据自己需要导入需要的包,在pyecharts的官方文档 https://pyecharts.org/#/ 中详细列出了绘制各种图表的的方法及参数含义,而且提供了各种图标的demo,方便我们更好地使用pyecharts。
from pyecharts.charts import Map from pyecharts import options as opts
3.代码
# 用于保存城市名称和确诊人数 map_data = [] for i in china : print(i) # 获得省份名称 province = i["name"] print("province:",province) province_confirm = i["total"]["confirm"] # 保存省份名称和该省确诊人数 map_data.append((i["name"],province_confirm)) c = ( # 声明一个map对象 Map() # 添加数据 .add("确诊", map_data, "china") # 设置标题和颜色 .set_global_opts(title_opts=opts.TitleOpts(title="全国疫情图"), visualmap_opts=opts.VisualMapOpts(split_number=6,is_piecewise=True, pieces=[{"min":1,"max":9,"label":"1-9人","color":"#ffefd7"}, {"min":10,"max":99,"label":"10-99人","color":"#ffd2a0"}, {"min":100,"max":499,"label":"100-499人","color":"#fe8664"}, {"min":500,"max":999,"label":"500-999人","color":"#e64b47"}, {"min":1000,"max":9999,"label":"1000-9999人","color":"#c91014"}, {"min":10000,"label":"10000人及以上","color":"#9c0a0d"} ])) ) # 生成html文件 c.render("全国实时疫情.html")
运行成功后就可以在工程目录下发现一个名为“全国实时疫情”的html文件,打开就可以看到我们绘制的疫情图啦!!
全部代码(包含保存到数据库,爬取数据、绘制疫情图):
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import requests import pymysql # 装了anaconda的可以pip install pyecharts安装pyecharts from pyecharts.charts import Map,Geo from pyecharts import options as opts from pyecharts.globals import GeoType,RenderType # 绘图包参加网址https://pyecharts.org/#/zh-cn/geography_charts id = 432 coon = pymysql.connect(user='root', password='root', host='127.0.0.1', port=3306, database='yiqing',use_unicode=True, charset="utf8") cursor = coon.cursor() url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5" resp=requests.get(url) html=resp.json() data=json.loads(html["data"]) time = data["lastUpdateTime"] data_info = time.split(' ')[0] detail_time = time.split(' ')[1] # 获取json数据的全国省份疫情情况数据 china=data["areaTree"][0]["children"] # 用于保存城市名称和确诊人数 map_data = [] for i in china : print(i) # 获得省份名称 province = i["name"] print("province:",province) province_confirm = i["total"]["confirm"] # 保存省份名称和该省确诊人数 map_data.append((i["name"],province_confirm)) # 各省份下有各市,获取各市的疫情数据 for child in i["children"]: print(child) # 获取城市名称 city = child["name"] print("city:",city) # 获取确诊人数 confirm = int(child["total"]["confirm"]) # 获取疑似人数 suspect = int(child["total"]["suspect"]) # 获取死亡人数 dead = int(child["total"]["dead"]) # 获取治愈人数 heal = int(child["total"]["heal"]) # 插入数据库中 cursor.execute("INSERT INTO city(id,city,confirm,suspect,dead,heal,province,date_info,detail_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)", (id, city, confirm, suspect, dead, heal, province, data_info, detail_time)) id = id + 1 coon.commit() c = ( # 声明一个map对象 Map() # 添加数据 .add("确诊", map_data, "china") # 设置标题和颜色 .set_global_opts(title_opts=opts.TitleOpts(title="全国疫情图"), visualmap_opts=opts.VisualMapOpts(split_number=6,is_piecewise=True, pieces=[{"min":1,"max":9,"label":"1-9人","color":"#ffefd7"}, {"min":10,"max":99,"label":"10-99人","color":"#ffd2a0"}, {"min":100,"max":499,"label":"100-499人","color":"#fe8664"}, {"min":500,"max":999,"label":"500-999人","color":"#e64b47"}, {"min":1000,"max":9999,"label":"1000-9999人","color":"#c91014"}, {"min":10000,"label":"10000人及以上","color":"#9c0a0d"} ])) ) # 生成html文件 c.render("全国实时疫情.html") # # china_total="确诊" + str(data["chinaTotal"]["confirm"])+ "疑似" + str(data["chinaTotal"]["suspect"])+ "死亡" + str(data["chinaTotal"]["dead"]) + "治愈" + str(data["chinaTotal"]["heal"]) + "更新日期" + data["lastUpdateTime"] # print(china_total)