本文原創連接:https:////www.cnblogs.com/zhanling/p/12192978.html
1 import numpy as np 2 import xarray as xr 3 import cartopy.crs as ccrs 4 import cartopy.feature as cfeat 5 from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER 6 import matplotlib.pyplot as plt 7 8 9 ds = xr.open_dataset('2039071310.003.nc') 10 t = ds['value'] 11 lons = ds.lon.data 12 lats = ds.lat.data 13 temp = xr.DataArray(t.data.T, coords=[lats,lons], dims=['latitude','longitude']) 14 15 # 建立畫圖空間 16 proj = ccrs.PlateCarree() #建立投影 17 fig = plt.figure(figsize=(16,9)) #建立頁面 18 ax = fig.subplots(1, 1, subplot_kw={'projection': proj}) #子圖 19 # 設置地圖屬性:加載國界、海岸線、河流、湖泊 20 ax.add_feature(cfeat.BORDERS.with_scale('50m'), linewidth=0.8, zorder=1) 21 ax.add_feature(cfeat.COASTLINE.with_scale('50m'), linewidth=0.6, zorder=1) 22 ax.add_feature(cfeat.RIVERS.with_scale('50m'), zorder=1) 23 ax.add_feature(cfeat.LAKES.with_scale('50m'), zorder=1) 24 # 設置網格點屬性 25 gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, 26 linewidth=1.2, color='k', alpha=0.5, linestyle='--') 27 gl.xlabels_top = False #關閉頂端標籤 28 gl.ylabels_right = False #關閉右側標籤 29 gl.xformatter = LONGITUDE_FORMATTER #x軸設爲經度格式 30 gl.yformatter = LATITUDE_FORMATTER #y軸設爲緯度格式 31 # 設置colorbar 32 cbar_kwargs = { 33 'orientation': 'horizontal', 34 'label': 'Potential', 35 'shrink': 0.8, 36 } 37 # 畫圖 38 levels = np.arange(0,1,0.1) 39 temp.plot.contourf(ax=ax, levels=levels, cmap='Spectral_r', 40 cbar_kwargs=cbar_kwargs, transform=ccrs.PlateCarree()) 41 plt.savefig('test.jpg')
示例效果(強對流機率預報結果):html
原文出處:https://www.cnblogs.com/zhanling/p/12192978.htmlgit