Python-지구과학-대기과학-가시화그림 시리즈(一)-xarray를 이용하여netCDF 파일을 읽고 그림을 그리기(코드+예시)

7302 단어
 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')

예제 효과(강대류 확률 예보 결과):
 

좋은 웹페이지 즐겨찾기