Python實(shí)現(xiàn)疫情地圖可視化
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,易于閱讀和編寫,同時(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ù)類型?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ù)類型{dic}’,type(dic))print(f’利用loads將json字符串轉(zhuǎn)化成Python數(shù)據(jù)類型{res}’,type(res))
dics = {'name':'Tom','age':23}result = json.dumps(dics)print(type(result))result
需求:爬取疫情的數(shù)據(jù)、如何處理json數(shù)據(jù)以及根據(jù)疫情數(shù)據(jù)如何利用pyecharts繪制疫情地圖。
import requestsimport json# 國內(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請求,獲取響應(yīng)數(shù)據(jù)response = requests.get(China_url,headers=headers).json()data = json.loads(response[’data’])# 保存數(shù)據(jù)with open(’./2021-02-03國內(nèi)疫情.json’,’w’,encoding=’utf-8’) as f: # 不采用ASCII編碼 f.write(json.dumps(data,ensure_ascii=False,indent=2))
爬取的數(shù)據(jù)保存格式為json,開頭的部分?jǐn)?shù)據(jù)如下:
無論是json數(shù)據(jù)存儲(chǔ)的,還是Python的基本數(shù)據(jù)類型存儲(chǔ)的,對于數(shù)據(jù)分析都不是很友好,所以我們可以將其數(shù)據(jù)存儲(chǔ)類型轉(zhuǎn)化為pandas的DataFrame類型,因?yàn)镈ataFrame和Excel可以更好的相互轉(zhuǎn)換。
生成的數(shù)據(jù)模式如下:
將以上的數(shù)據(jù)進(jìn)行處理,獲得Excel表一樣規(guī)范的數(shù)據(jù)格式。
import pandas as pdchinaTotalData = 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’] = confirmlistchinaTotalData[’suspect’] = suspectlistchinaTotalData[’dead’] = deadlistchinaTotalData[’heal’] = heallistchinaTotalData[’deadRate’] = deadRatelistchinaTotalData[’healRate’] = healRatelist# ===================================================================# 創(chuàng)建全國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_confirmlistchinaTotalData[’today_confirmCuts’] = today_confirmCutslist# ==================================================================# 刪除total、today兩列chinaTotalData.drop([’total’,’today’],axis=1,inplace=True)chinaTotalData.head()# 將其保存到Excel中chinaTotalData.to_excel(’2021-02-03國內(nèi)疫情.xlsx’,index=False)
處理好的數(shù)據(jù)結(jié)構(gòu)如下表:
pyecharts是一款將python與echarts結(jié)合的強(qiáng)大的數(shù)據(jù)可視化工具。繪制出來的圖比Python的Matplotlib簡單美觀。使用之前需要在Python環(huán)境中按照pycharts。在終端中輸入命令:pip install pyecharts
利用pyecharts繪制疫情地圖根據(jù)上面的疫情數(shù)據(jù),我們可以利用其畫出全國的疫情地圖在繪制前,我們需要安裝echarts的地圖包(可根據(jù)不同的地圖需求進(jìn)行安裝)
pip install echarts-countries-pypkgpip install echarts-china-provinces-pypkgpip install echarts-china-cities-pypkgpip install echarts-china-misc-pypkgpip install echarts-china-countries-pypkgpip install echarts-united-kingdom-pypkg
# 導(dǎo)入對應(yīng)的繪圖工具包import pandas as pdfrom pyecharts import options as optsfrom pyecharts.charts import Mapdf = pd.read_excel(’./2021-02-03國內(nèi)疫情.xlsx’)# 1.根據(jù)繪制國內(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 cd_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 疫情地圖可視化的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. springcloud alibaba nacos linux配置的詳細(xì)教程2. Vue為什么要謹(jǐn)慎使用$attrs與$listeners3. SpringBoot 使用 @Value 注解讀取配置文件給靜態(tài)變量賦值4. Java 生成帶Logo和文字的二維碼5. SpringBoot整合MybatisPlus的教程詳解6. 解決在Vue中使用axios POST請求變成OPTIONS的問題7. Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法8. python 實(shí)現(xiàn)圖片修復(fù)(可用于去水印)9. Android自定義控件實(shí)現(xiàn)方向盤效果10. python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件
