最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python抓新型冠狀病毒肺炎疫情數(shù)據(jù)并繪制全國疫情分布的代碼實(shí)例

 更新時間:2020年02月05日 14:53:58   作者:蒜泥的冬天  
在本篇文章里小編給大家整理了一篇關(guān)于Python抓新型冠狀病毒肺炎疫情數(shù)據(jù)并繪制全國疫情分布的代碼實(shí)例,有興趣的朋友們可以學(xué)習(xí)下。

運(yùn)行結(jié)果(2020-2-4日數(shù)據(jù))

數(shù)據(jù)來源

news.qq.com/zt2020/page/feiyan.htm

抓包分析

日報數(shù)據(jù)格式

"chinaDayList": [{
		"date": "01.13",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}, {
		"date": "01.14",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}, {
		"date": "01.15",
		"confirm": "41",
		"suspect": "0",
		"dead": "2",
		"heal": "5"
	}, {
	。。。。。。

全國各地疫情數(shù)據(jù)格式

	"lastUpdateTime": "2020-02-04 12:43:19",
	"areaTree": [{
		"name": "中國",
		"children": [{
			"name": "湖北",
			"children": [{
				"name": "武漢",
				"total": {
					"confirm": 6384,
					"suspect": 0,
					"dead": 313,
					"heal": 303
				},
				"today": {
					"confirm": 1242,
					"suspect": 0,
					"dead": 48,
					"heal": 79
				}
			}, {
				"name": "黃岡",
				"total": {
					"confirm": 1422,
					"suspect": 0,
					"dead": 19,
					"heal": 36
				},
				"today": {
					"confirm": 176,
					"suspect": 0,
					"dead": 2,
					"heal": 9
				}
			}, {
			。。。。。。

地圖數(shù)據(jù)

github.com/dongli/china-shapefiles

代碼實(shí)現(xiàn)

#%%

import time, json, requests
from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.font_manager import FontProperties
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
import numpy as np
import jsonpath

plt.rcParams['font.sans-serif'] = ['SimHei'] # 用來正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負(fù)號

#%%

# 全國疫情地區(qū)分布(省級確診病例)
def catch_cn_disease_dis():
 timestamp = '%d'%int(time.time()*1000)
 url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    '&callback=&_=') + timestamp
 world_data = json.loads(requests.get(url=url_area).json()['data'])
 china_data = jsonpath.jsonpath(world_data, 
         expr='$.areaTree[0].children[*]')
 list_province = jsonpath.jsonpath(china_data, expr='$[*].name')
 list_province_confirm = jsonpath.jsonpath(china_data, expr='$[*].total.confirm')
 dic_province_confirm = dict(zip(list_province, list_province_confirm)) 
 return dic_province_confirm

area_data = catch_cn_disease_dis()
print(area_data)

#%%

# 抓取全國疫情按日期分布
'''
數(shù)據(jù)源:
"chinaDayList": [{
		"date": "01.13",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}, {
		"date": "01.14",
		"confirm": "41",
		"suspect": "0",
		"dead": "1",
		"heal": "0"
	}
'''
def catch_cn_daily_dis():
 timestamp = '%d'%int(time.time()*1000)
 url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    '&callback=&_=') + timestamp
 world_data = json.loads(requests.get(url=url_area).json()['data'])
 china_daily_data = jsonpath.jsonpath(world_data, 
         expr='$.chinaDayList[*]')

 # 其實(shí)沒必要單獨(dú)用list存儲,json可讀性已經(jīng)很好了;這里這樣寫僅是為了少該點(diǎn)老版本的代碼  
 list_dates = list() # 日期
 list_confirms = list() # 確診
 list_suspects = list() # 疑似
 list_deads = list() # 死亡
 list_heals = list() # 治愈  
 for item in china_daily_data:
  month, day = item['date'].split('.')
  list_dates.append(datetime.strptime('2020-%s-%s'%(month, day), '%Y-%m-%d'))
  list_confirms.append(int(item['confirm']))
  list_suspects.append(int(item['suspect']))
  list_deads.append(int(item['dead']))
  list_heals.append(int(item['heal']))  
 return list_dates, list_confirms, list_suspects, list_deads, list_heals  

list_date, list_confirm, list_suspect, list_dead, list_heal = catch_cn_daily_dis() 
print(list_date)
 

#%%

# 繪制每日確診和死亡數(shù)據(jù)
def plot_cn_daily():
 # list_date, list_confirm, list_suspect, list_dead, list_heal = catch_cn_daily_dis() 
 
 plt.figure('novel coronavirus', facecolor='#f4f4f4', figsize=(10, 8))
 plt.title('全國新型冠狀病毒疫情曲線', fontsize=20)
 print('日期元素數(shù):', len(list_date), "\n確診元素數(shù):", len(list_confirm))
 plt.plot(list_date, list_confirm, label='確診')
 plt.plot(list_date, list_suspect, label='疑似')
 plt.plot(list_date, list_dead, label='死亡')
 plt.plot(list_date, list_heal, label='治愈')
 xaxis = plt.gca().xaxis 
 # x軸刻度為1天
 xaxis.set_major_locator(matplotlib.dates.DayLocator(bymonthday=None, interval=1, tz=None))
 xaxis.set_major_formatter(mdates.DateFormatter('%m月%d日'))
 plt.gcf().autofmt_xdate() # 優(yōu)化標(biāo)注(自動傾斜)
 plt.grid(linestyle=':') # 顯示網(wǎng)格
 plt.xlabel('日期',fontsize=16)
 plt.ylabel('人數(shù)',fontsize=16)
 plt.legend(loc='best')
 
plot_cn_daily()

#%%

# 繪制全國省級行政區(qū)域確診分布圖
count_iter = 0
def plot_cn_disease_dis():
 # area_data = catch_area_distribution()
 font = FontProperties(fname='res/coure.fon', size=14)
 
 # 經(jīng)緯度范圍
 lat_min = 10 # 緯度
 lat_max = 60
 lon_min = 70 # 經(jīng)度
 lon_max = 140
  
 # 標(biāo)簽顏色和文本 
 legend_handles = [
    matplotlib.patches.Patch(color='#7FFFAA', alpha=1, linewidth=0),
    matplotlib.patches.Patch(color='#ffaa85', alpha=1, linewidth=0),
    matplotlib.patches.Patch(color='#ff7b69', alpha=1, linewidth=0),
    matplotlib.patches.Patch(color='#bf2121', alpha=1, linewidth=0),
    matplotlib.patches.Patch(color='#7f1818', alpha=1, linewidth=0),
 ]
 legend_labels = ['0人', '1-10人', '11-100人', '101-1000人', '>1000人']

 fig = plt.figure(facecolor='#f4f4f4', figsize=(10, 8)) 
 # 新建區(qū)域
 axes = fig.add_axes((0.1, 0.1, 0.8, 0.8)) # left, bottom, width, height, figure的百分比,從figure 10%的位置開始繪制, 寬高是figure的80%
 axes.set_title('全國新型冠狀病毒疫情地圖(確診)', fontsize=20) # fontproperties=font 設(shè)置失敗 
 # bbox_to_anchor(num1, num2), num1用于控制legend的左右移動,值越大越向右邊移動,num2用于控制legend的上下移動,值越大,越向上移動。
 axes.legend(legend_handles, legend_labels, bbox_to_anchor=(0.5, -0.11), loc='lower center', ncol=5) # prop=font
 
 china_map = Basemap(llcrnrlon=lon_min, urcrnrlon=lon_max, llcrnrlat=lat_min, urcrnrlat=lat_max, resolution='l', ax=axes)
 # labels=[True,False,False,False] 分別代表 [left,right,top,bottom]
 china_map.drawparallels(np.arange(lat_min,lat_max,10), labels=[1,0,0,0]) # 畫經(jīng)度線
 china_map.drawmeridians(np.arange(lon_min,lon_max,10), labels=[0,0,0,1]) # 畫緯度線
 china_map.drawcoastlines(color='black') # 洲際線
 china_map.drawcountries(color='red') # 國界線
 china_map.drawmapboundary(fill_color = 'aqua')
 # 畫中國國內(nèi)省界和九段線
 china_map.readshapefile('res/china-shapefiles-master/china', 'province', drawbounds=True)
 china_map.readshapefile('res/china-shapefiles-master/china_nine_dotted_line', 'section', drawbounds=True)
 
 global count_iter
 count_iter = 0
 
 # 內(nèi)外循環(huán)不能對調(diào),地圖中每個省的數(shù)據(jù)有多條(繪制每一個shape,可以去查一下第一條“臺灣省”的數(shù)據(jù))
 for info, shape in zip(china_map.province_info, china_map.province):
  pname = info['OWNER'].strip('\x00')
  fcname = info['FCNAME'].strip('\x00')
  if pname != fcname: # 不繪制海島
   continue
  is_reported = False # 西藏沒有疫情,數(shù)據(jù)源就不取不到其數(shù)據(jù) 
  for prov_name in area_data.keys():    
   count_iter += 1
   if prov_name in pname:
    is_reported = True
    if area_data[prov_name] == 0:
     color = '#f0f0f0'
    elif area_data[prov_name] <= 10:
     color = '#ffaa85'
    elif area_data[prov_name] <= 100:
     color = '#ff7b69'
    elif area_data[prov_name] <= 1000:
     color = '#bf2121'
    else:
     color = '#7f1818'
    break
   
  if not is_reported:
   color = '#7FFFAA'
   
  poly = Polygon(shape, facecolor=color, edgecolor=color)
  axes.add_patch(poly)
  

plot_cn_disease_dis()
print('迭代次數(shù)', count_iter)

以上就是腳本之家小編整理的全部知識點(diǎn)內(nèi)容,感謝大家的學(xué)習(xí)和對腳本之家的支持。

相關(guān)文章

  • Python基于wxPython和FFmpeg開發(fā)一個視頻標(biāo)簽工具

    Python基于wxPython和FFmpeg開發(fā)一個視頻標(biāo)簽工具

    在當(dāng)今數(shù)字媒體時代,視頻內(nèi)容的管理和標(biāo)記變得越來越重要,無論是研究人員需要對實(shí)驗視頻進(jìn)行時間點(diǎn)標(biāo)記,還是個人用戶希望對家庭視頻進(jìn)行分類整理,一個高效的視頻標(biāo)簽工具都是不可或缺的,本文將詳細(xì)分析一個基于Python、wxPython和FFmpeg開發(fā)的視頻標(biāo)簽工具
    2025-04-04
  • python之文件讀取一行一行的方法

    python之文件讀取一行一行的方法

    今天小編就為大家分享一篇python之文件讀取一行一行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 簡單了解為什么python函數(shù)后有多個括號

    簡單了解為什么python函數(shù)后有多個括號

    這篇文章主要介紹了簡單了解為什么python函數(shù)后有多個括號,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Django框架安裝方法圖文詳解

    Django框架安裝方法圖文詳解

    這篇文章主要介紹了Django框架安裝方法,結(jié)合圖文與實(shí)例形式詳細(xì)分析了Django框架的下載、安裝簡單使用方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2019-11-11
  • 解決Python報錯No module named Crypto問題

    解決Python報錯No module named Crypto問題

    這篇文章主要介紹了解決Python報錯No module named“Crypto”問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python之使用adb shell命令啟動應(yīng)用的方法詳解

    Python之使用adb shell命令啟動應(yīng)用的方法詳解

    今天小編就為大家分享一篇Python之使用adb shell命令啟動應(yīng)用的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • pandas刪除指定行詳解

    pandas刪除指定行詳解

    這篇文章主要介紹了pandas刪除指定行的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python?matplotlib實(shí)戰(zhàn)之散點(diǎn)圖繪制

    Python?matplotlib實(shí)戰(zhàn)之散點(diǎn)圖繪制

    散點(diǎn)圖,又名點(diǎn)圖、散布圖、X-Y圖,是將所有的數(shù)據(jù)以點(diǎn)的形式展現(xiàn)在平面直角坐標(biāo)系上的統(tǒng)計圖表,本文主要為大家介紹了如何使用Matplotlib繪制散點(diǎn)圖,需要的可以參考下
    2023-08-08
  • 解決pycharm:unused import statement錯誤的問題

    解決pycharm:unused import statement錯誤的問題

    這篇文章主要介紹了解決pycharm:unused import statement錯誤的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • pytest通過assert進(jìn)行斷言的實(shí)現(xiàn)

    pytest通過assert進(jìn)行斷言的實(shí)現(xiàn)

    assert斷言是一種用于檢查代碼是否按預(yù)期工作的方法,在pytest中,assert斷言可以用于測試代碼的正確性,以確保代碼在運(yùn)行時按照預(yù)期工作,本文就來介紹一下如何使用,感興趣的可以了解下
    2023-12-12

最新評論

通渭县| 于都县| 南靖县| 达尔| 高阳县| 信丰县| 鹤壁市| 明水县| 铁力市| 军事| 肃宁县| 环江| 呈贡县| 自贡市| 潜江市| 武山县| 敦煌市| 鹤峰县| 克东县| 海原县| 南乐县| 盘山县| 泸定县| 霞浦县| 禄丰县| 建德市| 习水县| 砀山县| 临沧市| 左权县| 郧西县| 汝阳县| 新平| 会昌县| 大英县| 承德市| 临武县| 大姚县| 克拉玛依市| 灌云县| 红原县|