久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

Python實(shí)現(xiàn)疫情地圖可視化

瀏覽:38日期:2022-06-28 11:46:31
一、 json模塊

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))

Python實(shí)現(xiàn)疫情地圖可視化

dics = {'name':'Tom','age':23}result = json.dumps(dics)print(type(result))result

Python實(shí)現(xiàn)疫情地圖可視化

二、通過Python實(shí)現(xiàn)疫情地圖可視化

需求:爬取疫情的數(shù)據(jù)、如何處理json數(shù)據(jù)以及根據(jù)疫情數(shù)據(jù)如何利用pyecharts繪制疫情地圖。

Python實(shí)現(xiàn)疫情地圖可視化

Python實(shí)現(xiàn)疫情地圖可視化

1.數(shù)據(jù)的獲取(基于request模塊)

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ù)如下:

Python實(shí)現(xiàn)疫情地圖可視化

2.將json格式的數(shù)據(jù)保存到Excel

無論是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ù)模式如下:

Python實(shí)現(xiàn)疫情地圖可視化

將以上的數(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)如下表:

Python實(shí)現(xiàn)疫情地圖可視化

3.應(yīng)用pyecharts進(jìn)行數(shù)據(jù)可視化

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)行效果如下:

Python實(shí)現(xià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)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 综合一区二区三区 | 中文字幕观看 | 国产精品亚洲成在人线 | 成人中文视频 | 女人毛片a毛片久久人人 | 久久国内精品 | 99re国产| 精品国产一级毛片 | 国产精品女人视频 | 日韩一区在线播放 | 九色av| 欧美极品一区二区三区 | 国产精品视频999 | 91精品国产一区二区三区香蕉 | 欧美黑人做爰xxxⅹ 国产精品一区二区视频 | av片在线观看 | 在线观看欧美日韩 | 国产3区 | 成人免费视频网 | 亚洲精品午夜aaa久久久 | 国产一区精品视频 | 亚洲国产精品久久久久久 | 91精产国品一二三区在线观看 | 午夜在线视频 | 国产精品久久免费视频 | 天天干天天操 | 久久精品国产精品 | 亚洲国产精品视频 | 美日韩一区二区三区 | 99久久精品免费看国产免费粉嫩 | 99在线视频精品 | 久久国产精品一区 | 午夜免费福利视频 | 成人精品一区二区 | 国产激情视频在线 | 婷婷色国产偷v国产偷v小说 | 欧美成人精品一区二区男人看 | 仙人掌旅馆在线观看 | 国产一区二区精品丝袜 | 亚洲91精品 | 精品久久久久久久久久久久久久 |