Python疫情數(shù)據(jù)可視化分析
前言
本項(xiàng)目主要通過(guò)python的matplotlib pandas pyecharts等庫(kù)對(duì)疫情數(shù)據(jù)進(jìn)行可視化分析
數(shù)據(jù)來(lái)源:
- 本數(shù)據(jù)集來(lái)源于kaggle競(jìng)賽的開(kāi)源數(shù)據(jù)集,數(shù)據(jù)集地址
- 本數(shù)據(jù)集主要涉及到全球疫情統(tǒng)計(jì),包括確診、治愈、死亡、時(shí)間、國(guó)家、地區(qū)等信息
功能函數(shù)
讀取文件
df = pd.read_csv(r'C:\Users\Hasee\Desktop/covid_19_data.csv') df.head()
更換列名,便于查看
cols= ['序號(hào)','日期','省/州','國(guó)家','最近更新','確診','死亡','治愈'] df.columns = cols df.日期 = pd.to_datetime(df.日期) df
## 利用groupby按照日期統(tǒng)計(jì)確診死亡治愈病例的總和
#合并同一天同國(guó)家日期 global_confirm = df.groupby('日期')[['確診', '死亡', '治愈']].sum() global_confirm
全球疫情趨勢(shì)
ax = global_confirm.plot(figsize = (12,10), title = '全球疫情趨勢(shì)圖')
篩選出中國(guó)的數(shù)據(jù)
利用groupby按照日期統(tǒng)計(jì)確診死亡治愈病例的總和
global_china = df[df['國(guó)家'] == 'Mainland China'].reset_index() global_china_confirm = global_china.groupby('日期')[['確診', '死亡', '治愈']].sum().reset_index()
畫(huà)圖,三條線組合到一個(gè)圖
利用groupby按照省統(tǒng)計(jì)確診死亡治愈病例的總和
global_china = df[df['國(guó)家'] == 'Mainland China'].reset_index() global_china_province_confirm = global_china.groupby('省/州')[['確診', '死亡', '治愈']].sum().reset_index() recovercent = 100.*global_china_province_confirm['治愈'] / global_china_province_confirm['治愈'].sum() labels = ['{0}-{1:1.2f}%-{2}'.format(i,j,k) for i,j,k in zip(list(global_china_province_confirm['省/州']), recovercent, list(global_china_province_confirm['治愈']))] plt.figure(figsize=(10,10)) plt.pie(global_china_province_confirm['治愈'],radius = 0.3)
確診人數(shù)排名前15的國(guó)家
plt.figure(figsize=(16,16)) plt.barh(list(global_country_confirm_rank.國(guó)家)[::-1], list(global_country_confirm_rank.確診)[::-1]) plt.title('確診人數(shù)排名前15的國(guó)家') plt.xlabel('人數(shù)(千萬(wàn))') plt.ylabel('國(guó)家')
這里用pyecharts庫(kù)畫(huà)圖,繪制的玫瑰圖,rosetype
set_global_opts是設(shè)置格式:
中國(guó)確診人數(shù)前十的省
china_confirm = df[df['國(guó)家'] == "Mainland China"] china_latest = china_confirm[china_confirm['日期'] == max(china_confirm['日期'])] words = WordCloud() words.add('確診人數(shù)', [tuple(dic) for dic in zip(list(china_latest['省/州']),list(china_latest['確診']))], word_size_range=[20,100])
區(qū)域圖
china_death = df[df['國(guó)家'] == "Mainland China"] china_death_latest = china_death[china_death['日期'] == max(china_death['日期'])] china_death_latest = china_death_latest.groupby('省/州')[['確診', '死亡']].max().reset_index()
geo = Map() geo.add("中國(guó)死亡病例分布", [list(z) for z in zip(china_death_prodic,list(china_death_latest['死亡']))], "china") geo.set_global_opts(title_opts=opts.TitleOpts(title="全國(guó)各省死亡病例數(shù)據(jù)分布"),visualmap_opts=opts.VisualMapOpts(is_piecewise=True, pieces=[ {"min": 1500, "label": '>10000人', "color": "#6F171F"}, {"min": 500, "max": 15000, "label": '500-1000人', "color": "#C92C34"}, {"min": 100, "max": 499, "label": '100-499人', "color": "#E35B52"}, {"min": 10, "max": 99, "label": '10-99人', "color": "#F39E86"}, {"min": 1, "max": 9, "label": '1-9人', "color": "#FDEBD0"}])) geo.render_notebook()
熱力圖
geo = Geo() geo.add_schema(maptype="china") geo.add("中國(guó)死亡病例分布", [list(dic) for dic in zip(china_death_prodic,list(china_death_latest['死亡']))],type_=GeoType.EFFECT_SCATTER) geo.set_global_opts(visualmap_opts=opts.VisualMapOpts(),title_opts=opts.TitleOpts(title="全國(guó)各省死亡病例數(shù)據(jù)分布")) geo.render_notebook()
全球死亡人數(shù)地理分布情況
map = Map() map.set_global_opts(title_opts=opts.TitleOpts(title="全球死亡人數(shù)地理分布情況"),visualmap_opts=opts.VisualMapOpts(is_piecewise=True, pieces=[ {"min": 100001, "label": '>100001人', "color": "#6F171F"}, {"min": 10001, "max": 100000, "label": '10001-100000人', "color": "#C92C34"}, {"min": 1001, "max": 10000, "label": '1001-10000人', "color": "#E35B52"}, {"min": 101, "max": 10000, "label": '101-10000人', "color": "#F39E86"}, {"min": 1, "max": 100, "label": '1-100人', "color": "#FDEBD0"}])) map.add("全球死亡人數(shù)地理分布情況", [list(z) for z in zip(global_death_n,list(global_death['死亡']))], "world") map.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) map.render_notebook()
全球疫情頻率直方圖
global_confirm.plot.hist(alpha=0.5) plt.xlabel('人數(shù)(千萬(wàn))') plt.ylabel('出現(xiàn)頻率') plt.title('全球疫情頻率直方圖')
其他圖
陜西確診病例餅圖
陜西省確診病例數(shù)據(jù)分布
中國(guó)治愈病例玫瑰圖
到此這篇關(guān)于Python疫情數(shù)據(jù)可視化分析的文章就介紹到這了,更多相關(guān)Python可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析Python 簡(jiǎn)單工廠模式和工廠方法模式的優(yōu)缺點(diǎn)
這篇文章主要介紹了Python 工廠模式的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07python變量數(shù)據(jù)類(lèi)型和運(yùn)算符
這篇文章主要介紹了python變量數(shù)據(jù)類(lèi)型和運(yùn)算符,不同類(lèi)型的變量可以進(jìn)行的運(yùn)算是不同的,所以必須理解變量的類(lèi)型,下面文章的更多相關(guān)內(nèi)容介紹,需要的小伙伴可以參考一下2022-07-07python中文分詞,使用結(jié)巴分詞對(duì)python進(jìn)行分詞(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇python中文分詞,使用結(jié)巴分詞對(duì)python進(jìn)行分詞的實(shí)例講解。有比較好的參考價(jià)值,希望能給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11python實(shí)現(xiàn)的簡(jiǎn)單窗口倒計(jì)時(shí)界面實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)的簡(jiǎn)單窗口倒計(jì)時(shí)界面,實(shí)例分析了Python基于Tkinter操作windows窗口界面的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05Python退出While循環(huán)的3種方法舉例詳解
在每次循環(huán)結(jié)束后,我們需要檢查循環(huán)條件是否滿足。如果條件滿足,則繼續(xù)執(zhí)行循環(huán)體內(nèi)的代碼,否則退出循環(huán),這篇文章主要給大家介紹了關(guān)于Python退出While循環(huán)的3種方法,需要的朋友可以參考下2023-10-10