-
大小: 12.39MB文件類型: .zip金幣: 2下載: 0 次發布日期: 2024-01-29
- 語言: Python
- 標簽: python??matplotlib??
資源簡介
python繪制新型冠狀病毒疫情地圖與疫情曲線。
讀取騰訊的接口獲得數據,再使用matplotlib庫進行繪制。
讀取騰訊的接口獲得數據,再使用matplotlib庫進行繪制。
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
import?time??#?時間庫
import?json??#?處理json數據專用的庫
import?requests??#?可以訪問網址獲得數據的一個庫,爬蟲中應用較為廣泛
from?datetime?import?datetime??#?時間庫
import?numpy?as?np??#?python數字庫,強化python對數學相關內容的處理能力
import?matplotlib??#?python的matlib庫
import?matplotlib.figure??#?matplotlib的圖形庫
from?matplotlib.font_manager?import?FontProperties??#?matplotlib的字體庫
from?matplotlib.backends.backend_agg?import?FigureCanvasAgg??#?matplotlib的可視化終端庫
from?matplotlib.patches?import?Polygon??#?matplotlib圖形庫中的“多邊形”
from?matplotlib.collections?import?PatchCollection??#?matplotlib收集庫,用于集成檢驗每個小區域
from?mpl_toolkits.basemap?import?basemap??#?地圖庫
import?matplotlib.pyplot?as?plt??#?matplotlib的繪圖庫
import?matplotlib.dates?as?mdates??#?matplotlib的日期庫
plt.rcParams[‘font.sans-serif‘]?=?[‘FangSong‘]??#?設置默認字體
plt.rcParams[‘axes.unicode_minus‘]?=?False
def?catch_daily():
????“““抓取每日確診和死亡數據“““
????#?抓取數據的網址
????url?=?‘https://view.inews.qq.com/g2/getOnsInfo?name=wuwei_ww_cn_day_counts&callback=&_=%d‘?%?int(
????????time.time()*1000)
????#?獲取數據并進行格式化處理
????data?=?json.loads(requests.get(url=url).json()[‘data‘])
????#?數據排序
????data.sort(key=lambda?x:?x[‘date‘])
????#?初始化5個列表,用于存放相關數據
????date_list?=?list()??#?日期
????confirm_list?=?list()??#?確診
????suspect_list?=?list()??#?疑似
????dead_list?=?list()??#?死亡
????heal_list?=?list()??#?治愈
????#?遍歷獲得的數
????for?item?in?data:
????????#?獲取日期
????????month?day?=?item[‘date‘].split(‘.‘)
????????#?把相關數據放入數組中
????????date_list.append(datetime.strptime(‘2020-%s-%s‘?%
???????????????????????????????????????????(month?day)?‘%Y-%m-%d‘))
????????confirm_list.append(int(item[‘confirm‘]))
????????suspect_list.append(int(item[‘suspect‘]))
????????dead_list.append(int(item[‘dead‘]))
????????heal_list.append(int(item[‘heal‘]))
????return?date_list?confirm_list?suspect_list?dead_list?heal_list
def?catch_distribution():
????“““抓取行政區域確診分布數據“““
????data?=?{‘西藏‘:?0}
????url?=?‘https://view.inews.qq.com/g2/getOnsInfo?name=wuwei_ww_area_counts&callback=&_=%d‘?%?int(
????????time.time()*1000)
????for?item?in?json.loads(requests.get(url=url).json()[‘data‘]):
????????if?item[‘area‘]?not?in?data:
????????????data.update({item[‘area‘]:?0})
????????data[item[‘area‘]]?+=?int(item[‘confirm‘])
????return?data
def?plot_daily():
????“““繪制每日確診和死亡數據“““
????date_list?confirm_list?suspect_list?dead_list?heal_list?=?catch_daily()??#?獲取數據
????#?初始化圖表的基本信息
????plt.figure(‘2019-nCoV疫情統計圖表‘?facecolor=‘#f4f4f4‘?figsize=(10?8))
????plt.title(‘2019-nCoV疫情曲線‘?fontsize=20)
????plt.plot(date_list?confirm_list?label=‘確診‘)
????plt.plot(date_list?suspect_list?label=‘疑似‘)
????plt.plot(date_list?dead_list?label=‘死亡‘)
????plt.plot(date_list?heal_list?label=‘治愈‘)
????plt.gca().xaxis.set_major_formatter(mdates.DateFormatter(‘%m-%d‘))??#?格式化時間軸標注
????plt.gcf().autofmt_xdate()??#?優化標注(自動傾斜)
????plt.grid(linestyle=‘:‘)??#?顯示網格
????plt.legend(loc=‘best‘)??#?顯示圖例
????plt.savefig(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????6567??2020-01-28?13:15??nCoV.py
?????目錄???????????0??2020-01-28?10:06??res\
?????目錄???????????0??2018-11-04?17:41??res\shapefiles\
?????文件?????????169??2018-11-04?17:41??res\shapefiles\README.md
?????文件??????162995??2018-11-04?17:41??res\shapefiles\china.dbf
?????文件?????????145??2018-11-04?17:41??res\shapefiles\china.prj
?????文件?????3523112??2018-11-04?17:41??res\shapefiles\china.shp
?????文件???????11036??2018-11-04?17:41??res\shapefiles\china.shx
?????文件?????????386??2018-11-04?17:41??res\shapefiles\china_country.dbf
?????文件?????????145??2018-11-04?17:41??res\shapefiles\china_country.prj
?????文件??????967240??2018-11-04?17:41??res\shapefiles\china_country.shp
?????文件?????????108??2018-11-04?17:41??res\shapefiles\china_country.shx
?????文件?????????768??2018-11-04?17:41??res\shapefiles\china_nine_dotted_line.dbf
?????文件?????????145??2018-11-04?17:41??res\shapefiles\china_nine_dotted_line.prj
?????文件????????9156??2018-11-04?17:41??res\shapefiles\china_nine_dotted_line.shp
?????文件?????????180??2018-11-04?17:41??res\shapefiles\china_nine_dotted_line.shx
?????文件????????1702??2018-11-04?17:41??res\shapefiles\plot-sample.ncl
?????文件???????19859??2018-11-04?17:41??res\shapefiles\shapefile_utils.ncl
?????文件?????????386??2018-11-04?17:41??res\shapefiles\simplied_china_country.dbf
?????文件?????????145??2018-11-04?17:41??res\shapefiles\simplied_china_country.prj
?????文件????????9492??2018-11-04?17:41??res\shapefiles\simplied_china_country.shp
?????文件?????????108??2018-11-04?17:41??res\shapefiles\simplied_china_country.shx
?????文件????10512288??2008-04-14?20:00??res\simsun.ttc
?????文件????10499104??2012-05-14?11:30??res\simsun.ttf
評論
共有 條評論