Python實(shí)現(xiàn)疫情地圖可視化
一、 json模塊
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,易于閱讀和編寫(xiě),同時(shí)也易于機(jī)器解析和生成,并有效地提升網(wǎng)絡(luò)傳輸效率。
- json.loads():將json格式的str轉(zhuǎn)化成python的數(shù)據(jù)格式;
- json.loads():將python的數(shù)據(jù)格式(字典或列表)轉(zhuǎn)化成json格式;
# 如何將json數(shù)據(jù)解析成我們所熟悉的Python數(shù)據(jù)類(lèi)型? import json # 將json格式的str轉(zhuǎn)化成python的數(shù)據(jù)格式:字典 dic = json.loads('{"name":"Tom","age":23}') res = json.loads('["name","age","gender"]') print(f'利用loads將json字符串轉(zhuǎn)化成Python數(shù)據(jù)類(lèi)型{dic}',type(dic)) print(f'利用loads將json字符串轉(zhuǎn)化成Python數(shù)據(jù)類(lèi)型{res}',type(res))
dics = {"name":"Tom","age":23} result = json.dumps(dics) print(type(result)) result
二、通過(guò)Python實(shí)現(xiàn)疫情地圖可視化
需求:爬取疫情的數(shù)據(jù)、如何處理json數(shù)據(jù)以及根據(jù)疫情數(shù)據(jù)如何利用pyecharts繪制疫情地圖。
1.數(shù)據(jù)的獲取(基于request模塊)
import requests import json # 國(guó)內(nèi)疫情數(shù)據(jù) 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/', } # 發(fā)起get請(qǐng)求,獲取響應(yīng)數(shù)據(jù) response = requests.get(China_url,headers=headers).json() data = json.loads(response['data']) # 保存數(shù)據(jù) with open('./2021-02-03國(guó)內(nèi)疫情.json','w',encoding='utf-8') as f: # 不采用ASCII編碼 f.write(json.dumps(data,ensure_ascii=False,indent=2))
爬取的數(shù)據(jù)保存格式為json,開(kāi)頭的部分?jǐn)?shù)據(jù)如下:
2.將json格式的數(shù)據(jù)保存到Excel
無(wú)論是json數(shù)據(jù)存儲(chǔ)的,還是Python的基本數(shù)據(jù)類(lèi)型存儲(chǔ)的,對(duì)于數(shù)據(jù)分析都不是很友好,所以我們可以將其數(shù)據(jù)存儲(chǔ)類(lèi)型轉(zhuǎn)化為pandas的DataFrame類(lèi)型,因?yàn)镈ataFrame和Excel可以更好的相互轉(zhuǎn)換。
生成的數(shù)據(jù)模式如下:
將以上的數(shù)據(jù)進(jìn)行處理,獲得Excel表一樣規(guī)范的數(shù)據(jù)格式。
import pandas as pd chinaTotalData = pd.DataFrame(china_citylist) # 將整體數(shù)據(jù)chinaTotalData中的today和total數(shù)據(jù)添加到DataFrame中 # 處理total字典里面的各個(gè)數(shù)據(jù)項(xiàng) # ====================================================================== 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 # =================================================================== # 創(chuàng)建全國(guó)today數(shù)據(jù) 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國(guó)內(nèi)疫情.xlsx',index=False)
處理好的數(shù)據(jù)結(jié)構(gòu)如下表:
3.應(yīng)用pyecharts進(jìn)行數(shù)據(jù)可視化
pyecharts是一款將python與echarts結(jié)合的強(qiáng)大的數(shù)據(jù)可視化工具。繪制出來(lái)的圖比Python的Matplotlib簡(jiǎn)單美觀。使用之前需要在Python環(huán)境中按照pycharts。在終端中輸入命令:pip install pyecharts
利用pyecharts繪制疫情地圖
根據(jù)上面的疫情數(shù)據(jù),我們可以利用其畫(huà)出全國(guó)的疫情地圖
在繪制前,我們需要安裝echarts的地圖包(可根據(jù)不同的地圖需求進(jìn)行安裝)
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
# 導(dǎo)入對(duì)應(yīng)的繪圖工具包 import pandas as pd from pyecharts import options as opts from pyecharts.charts import Map df = pd.read_excel('./2021-02-03國(guó)內(nèi)疫情.xlsx') # 1.根據(jù)繪制國(guó)內(nèi)總疫情圖(確診) data = df.groupby(by='province',as_index=False).sum() data_list = list(zip(data['province'].values.tolist(),data['confirm'].values.tolist())) # 數(shù)據(jù)格式[(黑龍江,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")
最終的運(yùn)行效果如下:
注:以上的運(yùn)行環(huán)境是Python3.7版本,IDE是基于瀏覽器端的Jupter Notebook。
以上就是Python實(shí)現(xiàn)疫情地圖可視化的詳細(xì)內(nèi)容,更多關(guān)于python 疫情地圖可視化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python全棧之文件函數(shù)和函數(shù)參數(shù)
這篇文章主要為大家介紹了Python的文件函數(shù)和函數(shù)參數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12TensorFlow加載模型時(shí)出錯(cuò)的解決方式
今天小編就為大家分享一篇TensorFlow加載模型時(shí)出錯(cuò)的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python中numpy的np.percentile百分位函數(shù)舉例詳解
在python中計(jì)算一個(gè)多維數(shù)組的任意百分比分位數(shù),此處的百分位是從小到大排列,只需用np.percentile即可,這篇文章主要給大家介紹了關(guān)于Python中numpy的np.percentile百分位函數(shù)的相關(guān)資料,需要的朋友可以參考下2024-08-08python網(wǎng)絡(luò)爬蟲(chóng)之如何偽裝逃過(guò)反爬蟲(chóng)程序的方法
本篇文章主要介紹了python網(wǎng)絡(luò)爬蟲(chóng)之如何偽裝逃過(guò)反爬蟲(chóng)程序的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11一文教會(huì)你用Python實(shí)現(xiàn)pdf轉(zhuǎn)word
python實(shí)現(xiàn)pdf轉(zhuǎn)word,支持中英文轉(zhuǎn)換,轉(zhuǎn)換精度高,可以達(dá)到使用效果,下面這篇文章主要給大家介紹了關(guān)于用Python實(shí)現(xiàn)pdf轉(zhuǎn)word的相關(guān)資料,需要的朋友可以參考下2023-01-01快速排序的四種python實(shí)現(xiàn)(推薦)
這篇文章主要介紹了python實(shí)現(xiàn)快速排序算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Python中的 ansible 動(dòng)態(tài)Inventory 腳本
這篇文章主要介紹了Python中的 ansible 動(dòng)態(tài)Inventory 腳本,本章節(jié)通過(guò)實(shí)例代碼從mysql數(shù)據(jù)作為數(shù)據(jù)源生成動(dòng)態(tài)ansible主機(jī)為入口介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2020-01-01