Python繪圖實(shí)現(xiàn)臺(tái)風(fēng)路徑可視化代碼實(shí)例
臺(tái)風(fēng)是重大災(zāi)害性天氣,臺(tái)風(fēng)引起的直接災(zāi)害通常由三方面造成,狂風(fēng)、暴雨、風(fēng)暴潮,除此以外臺(tái)風(fēng)的這些災(zāi)害極易誘發(fā)城市內(nèi)澇、房屋倒塌、山洪、泥石流等次生災(zāi)害。正因如此,臺(tái)風(fēng)在科研和業(yè)務(wù)工作中是研究的重點(diǎn)。希望這次臺(tái)風(fēng)路徑可視化可以給予大家一點(diǎn)點(diǎn)幫助。
臺(tái)風(fēng)路徑的獲取
中國(guó)氣象局(CMA)
中國(guó)氣象局(CMA)的臺(tái)風(fēng)最佳路徑數(shù)據(jù)集(BST),BST是之后對(duì)歷史臺(tái)風(fēng)路徑進(jìn)行校正后發(fā)布的,其經(jīng)緯度、強(qiáng)度、氣壓具有更高的可靠性,但是時(shí)間分辨率為6小時(shí),部分3小時(shí),這一點(diǎn)不如觀測(cè)數(shù)據(jù)。下載地址:
溫州臺(tái)風(fēng)網(wǎng)
溫州臺(tái)風(fēng)網(wǎng)的數(shù)據(jù)是實(shí)時(shí)發(fā)布數(shù)據(jù)的記錄,時(shí)間分辨率最高達(dá)1小時(shí),對(duì)于臺(tái)風(fēng)軌跡具有更加精細(xì)化的表述。下載地址:
示例
導(dǎo)入模塊并讀取數(shù)據(jù),使用BST的2018年臺(tái)風(fēng)路徑數(shù)據(jù)作為示例,已經(jīng)將原始的txt文件轉(zhuǎn)換為xls文件。
import os, glob import pandas as pd import numpy as np import shapely.geometry as sgeom import matplotlib.pyplot as plt from matplotlib.image import imread from matplotlib.animation import FuncAnimation import matplotlib.lines as mlines import cartopy.crs as ccrs import cartopy.feature as cfeat from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter import cartopy.io.shapereader as shpreader import cartopy.io.img_tiles as cimgt from PIL import Image import warnings warnings.filterwarnings('ignore') df = pd.read_csv('./2018typhoon.csv')
定義等級(jí)色標(biāo)
def get_color(level): global color if level == '熱帶低壓' or level == '熱帶擾動(dòng)': color='#FFFF00' elif level == '熱帶風(fēng)暴': color='#6495ED' elif level == '強(qiáng)熱帶風(fēng)暴': color='#3CB371' elif level == '臺(tái)風(fēng)': color='#FFA500' elif level == '強(qiáng)臺(tái)風(fēng)': color='#FF00FF' elif level == '超強(qiáng)臺(tái)風(fēng)': color='#DC143C' return color
定義底圖函數(shù)
def create_map(title, extent): fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) url = 'http://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi' layer = 'BlueMarble_ShadedRelief' ax.add_wmts(url, layer) ax.set_extent(extent,crs=ccrs.PlateCarree()) gl = ax.gridlines(draw_labels=False, linewidth=1, color='k', alpha=0.5, linestyle='--') gl.xlabels_top = gl.ylabels_right = False ax.set_xticks(np.arange(extent[0], extent[1]+5, 5)) ax.set_yticks(np.arange(extent[2], extent[3]+5, 5)) ax.xaxis.set_major_formatter(LongitudeFormatter()) ax.xaxis.set_minor_locator(plt.MultipleLocator(1)) ax.yaxis.set_major_formatter(LatitudeFormatter()) ax.yaxis.set_minor_locator(plt.MultipleLocator(1)) ax.tick_params(axis='both', labelsize=10, direction='out') a = mlines.Line2D([],[],color='#FFFF00',marker='o',markersize=7, label='TD',ls='') b = mlines.Line2D([],[],color='#6495ED', marker='o',markersize=7, label='TS',ls='') c = mlines.Line2D([],[],color='#3CB371', marker='o',markersize=7, label='STS',ls='') d = mlines.Line2D([],[],color='#FFA500', marker='o',markersize=7, label='TY',ls='') e = mlines.Line2D([],[],color='#FF00FF', marker='o',markersize=7, label='STY',ls='') f = mlines.Line2D([],[],color='#DC143C', marker='o',markersize=7, label='SSTY',ls='') ax.legend(handles=[a,b,c,d,e,f], numpoints=1, handletextpad=0, loc='upper left', shadow=True) plt.title(f'{title} Typhoon Track', fontsize=15) return ax
定義繪制單個(gè)臺(tái)風(fēng)路徑方法,并繪制2018年第18號(hào)臺(tái)風(fēng)溫比亞。
def draw_single(df): ax = create_map(df['名字'].iloc[0], [110, 135, 20, 45]) for i in range(len(df)): ax.scatter(list(df['經(jīng)度'])[i], list(df['緯度'])[i], marker='o', s=20, color=get_color(list(df['強(qiáng)度'])[i])) for i in range(len(df)-1): pointA = list(df['經(jīng)度'])[i],list(df['緯度'])[i] pointB = list(df['經(jīng)度'])[i+1],list(df['緯度'])[i+1] ax.add_geometries([sgeom.LineString([pointA, pointB])], color=get_color(list(df['強(qiáng)度'])[i+1]),crs=ccrs.PlateCarree()) plt.savefig('./typhoon_one.png') draw_single(df[df['編號(hào)']==1818])
定義繪制多個(gè)臺(tái)風(fēng)路徑方法,并繪制2018年全年的全部臺(tái)風(fēng)路徑。
def draw_multi(df): L = list(set(df['編號(hào)'])) L.sort(key=list(df['編號(hào)']).index) ax = create_map('2018', [100, 180, 0, 45]) for number in L: df1 = df[df['編號(hào)']==number] for i in range(len(df1)-1): pointA = list(df1['經(jīng)度'])[i],list(df1['緯度'])[i] pointB = list(df1['經(jīng)度'])[i+1],list(df1['緯度'])[i+1] ax.add_geometries([sgeom.LineString([pointA, pointB])], color=get_color(list(df1['強(qiáng)度'])[i+1]),crs=ccrs.PlateCarree()) plt.savefig('./typhoon_multi.png') draw_multi(df)
定義繪制單個(gè)臺(tái)風(fēng)gif路徑演變方法,并繪制2018年第18號(hào)臺(tái)風(fēng)的gif路徑圖。
def draw_single_gif(df): for state in range(len(df.index))[:]: ax = create_map(f'{df["名字"].iloc[0]} {df["時(shí)間"].iloc[state]}', [110, 135, 20, 45]) for i in range(len(df[:state])): ax.scatter(df['經(jīng)度'].iloc[i], df['緯度'].iloc[i], marker='o', s=20, color=get_color(df['強(qiáng)度'].iloc[i])) for i in range(len(df[:state])-1): pointA = df['經(jīng)度'].iloc[i],df['緯度'].iloc[i] pointB = df['經(jīng)度'].iloc[i+1],df['緯度'].iloc[i+1] ax.add_geometries([sgeom.LineString([pointA, pointB])], color=get_color(df['強(qiáng)度'].iloc[i+1]),crs=ccrs.PlateCarree()) print(f'正在繪制第{state}張軌跡圖') plt.savefig(f'./{df["名字"].iloc[0]}{str(state).zfill(3)}.png', bbox_inches='tight') # 將圖片拼接成動(dòng)畫(huà) imgFiles = list(glob.glob(f'./{df["名字"].iloc[0]}*.png')) images = [Image.open(fn) for fn in imgFiles] im = images[0] filename = f'./track_{df["名字"].iloc[0]}.gif' im.save(fp=filename, format='gif', save_all=True, append_images=images[1:], duration=500) draw_single_gif(df[df['編號(hào)']==1818])
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 實(shí)現(xiàn)交換兩個(gè)列表元素的位置示例
今天小編就為大家分享一篇python 實(shí)現(xiàn)交換兩個(gè)列表元素的位置示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06python自動(dòng)計(jì)算圖像數(shù)據(jù)集的RGB均值
這篇文章主要為大家詳細(xì)介紹了python自動(dòng)計(jì)算圖像數(shù)據(jù)集的RGB均值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06python保留小數(shù)函數(shù)的幾種使用總結(jié)
本文主要介紹了python保留小數(shù)函數(shù)的幾種使用總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02pyspark.sql.DataFrame與pandas.DataFrame之間的相互轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇pyspark.sql.DataFrame與pandas.DataFrame之間的相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Python 實(shí)現(xiàn)將某一列設(shè)置為str類(lèi)型
這篇文章主要介紹了Python 實(shí)現(xiàn)將某一列設(shè)置為str類(lèi)型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Python3.5 創(chuàng)建文件的簡(jiǎn)單實(shí)例
下面小編就為大家分享一篇Python3.5 創(chuàng)建文件的簡(jiǎn)單實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04純Python開(kāi)發(fā)的nosql數(shù)據(jù)庫(kù)CodernityDB介紹和使用實(shí)例
這篇文章主要介紹了純Python開(kāi)發(fā)的nosql數(shù)據(jù)庫(kù)CodernityDB介紹和使用實(shí)例,本文實(shí)例包含數(shù)據(jù)插入、數(shù)據(jù)更新、數(shù)據(jù)刪除、數(shù)據(jù)查詢(xún)等,需要的朋友可以參考下2014-10-10Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn)
這篇文章主要介紹了Keras保存模型并載入模型繼續(xù)訓(xùn)練的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02