Posted in Python onFebruary 05, 2021
一、 json模块
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
- json.loads():将json格式的str转化成python的数据格式;
- json.loads():将python的数据格式(字典或列表)转化成json格式;
# 如何将json数据解析成我们所熟悉的Python数据类型? import json # 将json格式的str转化成python的数据格式:字典 dic = json.loads('{"name":"Tom","age":23}') res = json.loads('["name","age","gender"]') print(f'利用loads将json字符串转化成Python数据类型{dic}',type(dic)) print(f'利用loads将json字符串转化成Python数据类型{res}',type(res))
dics = {"name":"Tom","age":23} result = json.dumps(dics) print(type(result)) result
二、通过Python实现疫情地图可视化
需求:爬取疫情的数据、如何处理json数据以及根据疫情数据如何利用pyecharts绘制疫情地图。
1.数据的获取(基于request模块)
import requests import json # 国内疫情数据 China_url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5' headers = { # 浏览器伪装 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36', 'referer': 'https://news.qq.com/', } # 发起get请求,获取响应数据 response = requests.get(China_url,headers=headers).json() data = json.loads(response['data']) # 保存数据 with open('./2021-02-03国内疫情.json','w',encoding='utf-8') as f: # 不采用ASCII编码 f.write(json.dumps(data,ensure_ascii=False,indent=2))
爬取的数据保存格式为json,开头的部分数据如下:
2.将json格式的数据保存到Excel
无论是json数据存储的,还是Python的基本数据类型存储的,对于数据分析都不是很友好,所以我们可以将其数据存储类型转化为pandas的DataFrame类型,因为DataFrame和Excel可以更好的相互转换。
生成的数据模式如下:
将以上的数据进行处理,获得Excel表一样规范的数据格式。
import pandas as pd chinaTotalData = pd.DataFrame(china_citylist) # 将整体数据chinaTotalData中的today和total数据添加到DataFrame中 # 处理total字典里面的各个数据项 # ====================================================================== confirmlist = [] suspectlist = [] deadlist = [] heallist = [] deadRatelist = [] healRatelist = [] # print(chinaTotalData['total'].values.tolist()[0]) for value in chinaTotalData['total'].values.tolist(): confirmlist.append(value['confirm']) suspectlist.append(value['suspect']) deadlist.append(value['dead']) heallist.append(value['heal']) deadRatelist.append(value['deadRate']) healRatelist.append(value['healRate']) chinaTotalData['confirm'] = confirmlist chinaTotalData['suspect'] = suspectlist chinaTotalData['dead'] = deadlist chinaTotalData['heal'] = heallist chinaTotalData['deadRate'] = deadRatelist chinaTotalData['healRate'] = healRatelist # =================================================================== # 创建全国today数据 today_confirmlist = [] today_confirmCutslist = [] for value in chinaTotalData['today'].values.tolist(): today_confirmlist.append(value['confirm']) today_confirmCutslist.append(value['confirmCuts']) chinaTotalData['today_confirm'] = today_confirmlist chinaTotalData['today_confirmCuts'] = today_confirmCutslist # ================================================================== # 删除total、today两列 chinaTotalData.drop(['total','today'],axis=1,inplace=True) chinaTotalData.head() # 将其保存到Excel中 chinaTotalData.to_excel('2021-02-03国内疫情.xlsx',index=False)
处理好的数据结构如下表:
3.应用pyecharts进行数据可视化
pyecharts是一款将python与echarts结合的强大的数据可视化工具。绘制出来的图比Python的Matplotlib简单美观。使用之前需要在Python环境中按照pycharts。在终端中输入命令:pip install pyecharts
利用pyecharts绘制疫情地图
根据上面的疫情数据,我们可以利用其画出全国的疫情地图
在绘制前,我们需要安装echarts的地图包(可根据不同的地图需求进行安装)
pip install echarts-countries-pypkg pip install echarts-china-provinces-pypkg pip install echarts-china-cities-pypkg pip install echarts-china-misc-pypkg pip install echarts-china-countries-pypkg pip install echarts-united-kingdom-pypkg
# 导入对应的绘图工具包 import pandas as pd from pyecharts import options as opts from pyecharts.charts import Map df = pd.read_excel('./2021-02-03国内疫情.xlsx') # 1.根据绘制国内总疫情图(确诊) data = df.groupby(by='province',as_index=False).sum() data_list = list(zip(data['province'].values.tolist(),data['confirm'].values.tolist())) # 数据格式[(黑龙江,200),(吉林,300),...] def map_china() -> Map: c = ( Map() .add(series_name="确诊病例",data_pair=data_list,maptype='china') .set_global_opts( title_opts = opts.TitleOpts(title='疫情地图'), visualmap_opts=opts.VisualMapOpts(is_piecewise=True, pieces = [{"max":9, "min":0, "label":"0-9","color":"#FFE4E1"}, {"max":99, "min":10, "label":"10-99","color":"#FF7F50"}, {"max":499, "min":100, "label":"100-4999","color":"#F08080"}, {"max":999, "min":500, "label":"500-999","color":"#CD5C5C"}, {"max":9999, "min":1000, "label":"1000-9999","color":"#990000"}, {"max":99999, "min":10000, "label":"10000-99999","color":"#660000"},] ) ) ) return c d_map = map_china() d_map.render("mapEchrts.html")
最终的运行效果如下:
注:以上的运行环境是Python3.7版本,IDE是基于浏览器端的Jupter Notebook。
以上就是Python实现疫情地图可视化的详细内容,更多关于python 疫情地图可视化的资料请关注三水点靠木其它相关文章!
Python实现疫情地图可视化
- Author -
TrainingL声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@