亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:41日期:2022-06-28 11:46:31
一、 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ù)類型?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)疫情地圖可視化

二、通過(guò)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# 國(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ù)如下:

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

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

無(wú)論是json數(shù)據(jù)存儲(chǔ)的,還是Python的基本數(shù)據(jù)類型存儲(chǔ)的,對(duì)于數(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)建全國(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_confirmlistchinaTotalData[’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)如下表:

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

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-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)入對(duì)應(yīng)的繪圖工具包import pandas as pdfrom pyecharts import options as optsfrom pyecharts.charts import Mapdf = 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 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 疫情地圖可視化的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久青草18免费观看网站 | 国产美女免费网站 | 国产一区二区三区四卡 | 国产成人精品男人的天堂网站 | 久青草国产手机视频免费观看 | 亚洲欧美一区二区三区在线 | 狠狠色香婷婷久久亚洲精品 | 我要看黄色录像一级片 | 欧美高清色视频在线播放 | 一区二区三区四区日韩 | 午夜性刺激免费视频观看不卡专区 | 欧美一级欧美三级 | 女人洗澡一级特黄毛片 | 欧美一区二区三区不卡免费 | 亚洲丁香 | 亚洲不卡免费视频 | 欧美a级v片在线观看一区 | 热综合一本伊人久久精品 | 中文字幕欧美日韩在线不卡 | 亚洲欧美综合 | 亚洲综合网国产福利精品一区 | 国产精品伦理久久久久 | 亚洲国产日韩在线人高清不卡 | 伊人色在线观看 | 国产成人深夜福利短视频99 | 亚洲制服在线观看 | 一区国产传媒国产精品 | 国产成人免费观看在线视频 | 全黄一级裸片视频免费 | 宅男69免费永久网站 | 国产毛片一级 | 久久精品嫩草影院 | 麻豆视频在线播放 | 日韩欧美一区二区久久黑人 | 黄页网址大全免费观看不用 | 欧美亚洲人成网站在线观看刚交 | 国产毛片儿 | 久久一区二区三区精品 | 精品国产欧美一区二区五十路 | 在线日韩视频 | 正在播放国产大学生情侣 |