数据分析---疫情数据可视化(地图)
安装pyecharts库
pyecharts属于第三方库,需要从https://pypi.org/project/pyecharts/#files 手动下载安装
下载后,打开“Anaconda Command Prompt”
输入:pip install 所在路径+pyecharts-1.7.0-py3-none-any.whl
安装后可再输入:pip list
查看是否安装成功
import requests
import json
from pyecharts.charts import Map, Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType, RenderType
网上有很多博主的文章写的是from pyecharts import Map, Geo ;这是因为版本不同,现在需要加“.charts”
爬取所需要的数据(网上直接爬取数据)
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
datas = json.loads(requests.get(url=url).json()['data'])
china = datas['areaTree'][0]['children']
data = []
for i in range(len(china)):
data.append([china[i]['name'], china[i]['total']['confirm']])
注:今天犯了一个小错误,我在往列表中添加元素时,原来打成了data=data.append()结果报错“AttributeError: ‘NoneType’ object has no attribute ‘append’”,这是因为append会修改data本身,并且返回None。不能把返回值再赋值给data。
print(data)
[['湖北', 67786], ['广东', 1356], ['河南', 1273], ['浙江', 1227], ['湖南', 1018], ['安徽', 990], ['江西', 935], ['山东', 760], ['江苏', 631], ['重庆', 576], ['四川', 539], ['黑龙江', 482], ['北京', 437], ['上海', 350], ['河北', 318], ['福建', 296], ['广西', 252], ['陕西', 245], ['云南', 174], ['海南', 168], ['贵州', 146], ['香港', 137], ['天津', 136], ['山西', 133], ['甘肃', 129], ['辽宁', 125], ['吉林', 93], ['新疆', 76], ['宁夏', 75], ['内蒙古', 75], ['台湾', 50], ['青海', 18], ['澳门', 10], ['西藏', 1]]
confirm = datas['chinaTotal']['confirm']
suspect = datas['chinaTotal']['suspect']
dead = datas['chinaTotal']['dead']
heal = datas['chinaTotal']['heal']
lastUpdateTime = datas['lastUpdateTime']
print(confirm, suspect, dead, lastUpdateTime)
81017 147 3181 2020-03-14 10:03:41
china_total = "确诊:" + str(confirm) + " 疑似:" + str(suspect) + " 死亡:" + str(dead) + " 治愈:" + str(heal) + " 更新日期:" + lastUpdateTime
hubei = datas['areaTree'][0]['children'][0]['children']
print(hubei)
[{'name': '武汉', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 49991, 'suspect': 0, 'dead': 2436, 'deadRate': '4.87', 'showRate': False, 'heal': 35197, 'healRate': '70.41', 'showHeal': True}}, {'name': '孝感', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 3518, 'suspect': 0, 'dead': 126, 'deadRate': '3.58', 'showRate': False, 'heal': 3204, 'healRate': '91.07', 'showHeal': True}}, {'name': '黄冈', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 2907, 'suspect': 0, 'dead': 125, 'deadRate': '4.30', 'showRate': False, 'heal': 2720, 'healRate': '93.57', 'showHeal': True}}, {'name': '荆州', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 1580, 'suspect': 0, 'dead': 49, 'deadRate': '3.10', 'showRate': False, 'heal': 1464, 'healRate': '92.66', 'showHeal': True}}, {'name': '鄂州', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 1394, 'suspect': 0, 'dead': 55, 'deadRate': '3.95', 'showRate': False, 'heal': 1184, 'healRate': '84.94', 'showHeal': True}}, {'name': '随州', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 1307, 'suspect': 0, 'dead': 45, 'deadRate': '3.44', 'showRate': False, 'heal': 1181, 'healRate': '90.36', 'showHeal': True}}, {'name': '襄阳', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 1175, 'suspect': 0, 'dead': 38, 'deadRate': '3.23', 'showRate': False, 'heal': 1112, 'healRate': '94.64', 'showHeal': True}}, {'name': '黄石', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 1015, 'suspect': 0, 'dead': 38, 'deadRate': '3.74', 'showRate': False, 'heal': 938, 'healRate': '92.41', 'showHeal': True}}, {'name': '宜昌', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 931, 'suspect': 0, 'dead': 36, 'deadRate': '3.87', 'showRate': False, 'heal': 847, 'healRate': '90.98', 'showHeal': True}}, {'name': '荆门', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 928, 'suspect': 0, 'dead': 39, 'deadRate': '4.20', 'showRate': False, 'heal': 839, 'healRate': '90.41', 'showHeal': True}}, {'name': '咸宁', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 836, 'suspect': 0, 'dead': 14, 'deadRate': '1.67', 'showRate': False, 'heal': 816, 'healRate': '97.61', 'showHeal': True}}, {'name': '十堰', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 672, 'suspect': 0, 'dead': 8, 'deadRate': '1.19', 'showRate': False, 'heal': 626, 'healRate': '93.15', 'showHeal': True}}, {'name': '仙桃', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 575, 'suspect': 0, 'dead': 22, 'deadRate': '3.83', 'showRate': False, 'heal': 520, 'healRate': '90.43', 'showHeal': True}}, {'name': '天门', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 496, 'suspect': 0, 'dead': 15, 'deadRate': '3.02', 'showRate': False, 'heal': 476, 'healRate': '95.97', 'showHeal': True}}, {'name': '恩施州', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 252, 'suspect': 0, 'dead': 7, 'deadRate': '2.78', 'showRate': False, 'heal': 236, 'healRate': '93.65', 'showHeal': True}}, {'name': '潜江', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 198, 'suspect': 0, 'dead': 9, 'deadRate': '4.55', 'showRate': False, 'heal': 182, 'healRate': '91.92', 'showHeal': True}}, {'name': '神农架', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'confirm': 11, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 11, 'healRate': '100.00', 'showHeal': True}}]
hubei_data = []
for j in range(len(hubei)):
hubei_data.append([hubei[j]['name'], hubei[j]['total']['confirm']])
print(hubei_data)
[['武汉', 49991], ['孝感', 3518], ['黄冈', 2907], ['荆州', 1580], ['鄂州', 1394], ['随州', 1307], ['襄阳', 1175], ['黄石', 1015], ['宜昌', 931], ['荆门', 928], ['咸宁', 836], ['十堰', 672], ['仙桃', 575], ['天门', 496], ['恩施州', 252], ['潜江', 198], ['神农架', 11]]
hubeitotal = datas['areaTree'][0]['children'][0]['total']
hubei_confirm = hubeitotal['confirm']
hubei_suspect = hubeitotal['suspect']
hubei_dead = hubeitotal['dead']
hubei_heal = hubeitotal['heal']
print(hubei_confirm, hubei_suspect, hubei_dead, lastUpdateTime)
hubei_total = "确诊:" + str(hubei_confirm) + " 疑似:" + str(hubei_suspect) + " 死亡:" + str(hubei_dead) + " 治愈:" + str(hubei_heal) + " 更新日期:" + lastUpdateTime
print(hubei_total)
67786 0 3062 2020-03-14 10:03:41
确诊:67786 疑似:0 死亡:3062 治愈:51553 更新日期:2020-03-14 10:03:41
绘制全国地图
设置绘图尺寸,背景色,页面标题,绘制类型;
附上RGB颜色对照表:https://www.114la.com/other/rgb.htm
geo_china = Geo(init_opts=opts.InitOpts(width="1680px", height="800px", bg_color="#EAEAEA", page_title="全国疫情实时报告",renderer=RenderType.SVG, theme="white"))
选择地图板块,设置地图区域颜色、区域边界颜色
geo_china = geo_china.add_schema(maptype="china", itemstyle_opts=opts.ItemStyleOpts(color="#FFFFFF",border_color="#030303"))
设置地图数据,动画方式为涟漪特效effect scatter
geo_china = geo_china.add(series_name="全国", data_pair=data, type_=GeoType.EFFECT_SCATTER)
设置系列配置;opts.LabelOpts(is_show=False/True)设置是否显示标签;effect_opts设置涟漪特效缩放比例
geo_china = geo_china.set_series_opts(label_opts=opts.LabelOpts(is_show=False),effect_opts=opts.EffectOpts(scale=6))
设置全局系列配置:设置标题,副标题,标题位置,文字颜色,opts.LegendOpts(is_show=True/False)设置“series_name”是否显示
geo_china = geo_china.set_global_opts(title_opts=opts.TitleOpts(title="全国疫情地图", subtitle=china_total,
pos_left="center", pos_top="30px",
title_textstyle_opts=opts.TextStyleOpts(color="#030303")),
legend_opts=opts.LegendOpts(is_show=False))
绘制省份地图(以湖北省为例)
geo_hubei = (Geo(init_opts=opts.InitOpts(width="1200px", height="800px", bg_color="#EAEAEA", page_title="湖北省疫情实时报告",renderer=RenderType.SVG, theme="white"))
.add_schema(maptype="湖北", itemstyle_opts=opts.ItemStyleOpts(color="#FFFFFF",border_color="#030303"))
.add(series_name="湖北", data_pair=hubei_data, type_=GeoType.EFFECT_SCATTER)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),effect_opts=opts.EffectOpts(scale=6))
.set_global_opts(title_opts=opts.TitleOpts(title="湖北省疫情地图", subtitle=hubei_total,
pos_left="center", pos_top="30px",
title_textstyle_opts=opts.TextStyleOpts(color="#030303")),
legend_opts=opts.LegendOpts(is_show=False)))
在全国地图上加入湖北省数据
geo_chinahubei = geo_china.add(series_name="湖北", data_pair=hubei_data, type_=GeoType.EFFECT_SCATTER)
geo_chinahubei = geo_chinahubei.set_global_opts(title_opts=opts.TitleOpts(title="全国和湖北省疫情地图", subtitle=china_total,
pos_left="center", pos_top="30px",
title_textstyle_opts=opts.TextStyleOpts(color="#030303")),
legend_opts=opts.LegendOpts(is_show=True))
geo_chinahubei = geo_chinahubei.set_series_opts(label_opts=opts.LabelOpts(is_show=False),effect_opts=opts.EffectOpts(scale=6))
导入数据(本地导入数据)
除了网上爬取数据还可以从本地直接读取已有数据
import xlrd
file = xlrd.open_workbook('F:\\公众号\\python\\【案例】数据分析---疫情数据可视化(地图)\\中国疫情数据.xlsx')
table = file.sheets()[0]
data = []
for i in range(1,len(table.col_values(0))):
data.append([table.cell(i,0).value,table.cell(i,1).value])
print(data)
[['湖北', 67773.0], ['广东', 1353.0], ['河南', 1272.0], ['浙江', 1215.0], ['湖南', 1018.0], ['安徽', 990.0], ['江西', 935.0], ['山东', 759.0], ['江苏', 631.0], ['重庆', 576.0], ['四川', 539.0], ['黑龙江', 482.0], ['北京', 435.0], ['上海', 344.0], ['河北', 318.0], ['福建', 296.0], ['广西', 252.0], ['陕西', 245.0], ['云南', 174.0], ['海南', 168.0], ['贵州', 146.0], ['天津', 136.0], ['山西', 133.0], ['甘肃', 125.0], ['辽宁', 125.0], ['香港', 120.0], ['吉林', 93.0], ['新疆', 76.0], ['内蒙古', 75.0], ['宁夏', 75.0], ['台湾', 47.0], ['青海', 18.0], ['澳门', 10.0], ['西藏', 1.0]]
总结:
今天给大家分享了geo()的一些常用参数设置,如何设置画布、如何设置标题标签、如何设置动画效果,但是geo()中仍有很多参数并未涉及,这里也无法给大家一一展示,我从CSDN上找了两篇较为详细的geo()参数介绍,附在文末,感兴趣的朋友可以自行研究,在以后的可视化案例中,也会尽可能多使用其它参数进行展示。
参考博客:
https://blog.csdn.net/qq_22592457/article/details/98957767
https://blog.csdn.net/chunbali9596/article/details/100819631?depth_1-utm_source=distribute.pc_relevant_right.none-task&utm_source=distribute.pc_relevant_right.none-task
获取代码
以下是我的个人公众号,该篇的代码可在公众号中回复“疫情数据可视化”即可获得,谢谢大家支持。