import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LatitudeFormatter,LongitudeFormatter
nc = xr.open_dataset('D:\\APP/progress/data/data/sst/cobesst.nc')
nc
C:\Users\kaiyo\anaconda3\lib\site-packages\xarray\coding\times.py:123: SerializationWarning: Ambiguous reference date string: 1-1-1 00:00:00. The first value is assumed to be the year hence will be padded with zeros to remove the ambiguity (the padded reference date string is: 0001-1-1 00:00:00). To remove this message, remove the ambiguity by padding your reference date strings with zeros. warnings.warn(warning_msg, SerializationWarning)
<xarray.Dataset> Dimensions: (time: 1557, lon: 360, lat: 180) Coordinates: * time (time) datetime64[ns] 1891-01-01 1891-02-01 ... 2020-09-01 * lon (lon) float64 0.5 1.5 2.5 3.5 4.5 ... 355.5 356.5 357.5 358.5 359.5 * lat (lat) float64 -89.5 -88.5 -87.5 -86.5 -85.5 ... 86.5 87.5 88.5 89.5 Data variables: t (time, lat, lon) float32 ... Attributes: CDI: Climate Data Interface version 1.9.8 (https://mpimet.mpg.de... Conventions: CF-1.6 history: Wed Nov 11 09:56:10 2020: cdo -f nc import_binary cobesst.c... CDO: Climate Data Operators version 1.9.8 (https://mpimet.mpg.de...
array(['1891-01-01T00:00:00.000000000', '1891-02-01T00:00:00.000000000', '1891-03-01T00:00:00.000000000', ..., '2020-07-01T00:00:00.000000000', '2020-08-01T00:00:00.000000000', '2020-09-01T00:00:00.000000000'], dtype='datetime64[ns]')
array([ 0.5, 1.5, 2.5, ..., 357.5, 358.5, 359.5])
array([-89.5, -88.5, -87.5, -86.5, -85.5, -84.5, -83.5, -82.5, -81.5, -80.5, -79.5, -78.5, -77.5, -76.5, -75.5, -74.5, -73.5, -72.5, -71.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -64.5, -63.5, -62.5, -61.5, -60.5, -59.5, -58.5, -57.5, -56.5, -55.5, -54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5, -46.5, -45.5, -44.5, -43.5, -42.5, -41.5, -40.5, -39.5, -38.5, -37.5, -36.5, -35.5, -34.5, -33.5, -32.5, -31.5, -30.5, -29.5, -28.5, -27.5, -26.5, -25.5, -24.5, -23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5, 30.5, 31.5, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5, 50.5, 51.5, 52.5, 53.5, 54.5, 55.5, 56.5, 57.5, 58.5, 59.5, 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5, 79.5, 80.5, 81.5, 82.5, 83.5, 84.5, 85.5, 86.5, 87.5, 88.5, 89.5])
[100893600 values with dtype=float32]
### 変数を定義 ###
lon = nc["lon"]
lat = nc["lat"]
sst = nc['t']
### 特定の日付のみ取り出す ###
year = sst['time'].dt.year
month = sst['time'].dt.month
sst_april = sst[(year==1998)&(month==4)]
### 不要な次元を削除 ###
sst_april = np.squeeze(sst_april)
fig = plt.figure()
ax = fig.add_subplot(1,1,1,projection=ccrs.PlateCarree(central_longitude= 180))
ax.add_feature(cfeature.LAND) ### 地形を塗る
ax.coastlines(resolution='50m',lw=0.5)
ax.set_title('SST (April 1998)',fontsize=20)
### 目盛を描く緯度経度の値を設定 ###
ax.set_xticks(np.arange(0,360.1,60),crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(-90,90.1,30),crs=ccrs.PlateCarree())
### 目盛の表示形式を度数表記にする ###
latfmt = LatitudeFormatter()
lonfmt = LongitudeFormatter(zero_direction_label=True)
ax.xaxis.set_major_formatter(lonfmt)
ax.yaxis.set_major_formatter(latfmt)
ax.axes.tick_params(labelsize=16)
### 描画範囲を指定 ###
ax.set_extent([120,270,10,70],ccrs.PlateCarree())
### 等値線 ###
clevs = np.arange(0,35,5)
im = ax.contour(lon,lat,sst_april,clevs,transform=ccrs.PlateCarree(),colors='k',extend='both')
im.clabel(fmt='%1.0f',fontsize=14)
### Shade ###
im = ax.contourf(lon,lat,sst_april,clevs,transform=ccrs.PlateCarree(),cmap="Spectral_r",extend="both")
### Colorbar ###
cb = plt.colorbar(im, pad=0.15, aspect=20, shrink=0.7,orientation="horizontal")
cb.set_label("[$^\circ$C]",labelpad=5,size=16)
cb.ax.tick_params(labelsize=16)
plt.show()
### 複数枚書いてみる ###
sst_98 = sst[(year==1998)]
def main():
title = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
fig = plt.figure(figsize=(30,5))
for i in range(0,12):
ax = fig.add_subplot(2,6,i+1,projection=ccrs.PlateCarree(central_longitude= 195))
ax.add_feature(cfeature.LAND)
ax.set_title(title[i],fontsize=18)
ax.coastlines(resolution='50m',lw=0.5)
### 目盛を描く緯度経度の値を設定 ###
ax.set_xticks(np.arange(0,360.1,60),crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(-90,90.1,30),crs=ccrs.PlateCarree())
### 目盛の表示形式を度数表記にする ###
latfmt = LatitudeFormatter()
lonfmt = LongitudeFormatter(zero_direction_label=True)
ax.xaxis.set_major_formatter(lonfmt)
ax.yaxis.set_major_formatter(latfmt)
ax.axes.tick_params(labelsize=15)
### 描画範囲を指定 ###
ax.set_extent([120,270,10,70],ccrs.PlateCarree())
clevs = np.arange(0,35,5)
im = ax.contourf(lon,lat,sst_98[(i)],clevs,transform=ccrs.PlateCarree(),cmap="Spectral_r",extend="both")
cax = plt.axes([0.26,0.05,0.5,0.05]) #[左下のx位置,左下のy位置,図に対する横の長さの比,図に対する縦の長さの比]
cb = plt.colorbar(im,cax=cax,orientation="horizontal")
cb.set_label("[$^\circ$C]",labelpad=5,size=15)
cb.ax.tick_params(labelsize=15)
fig.suptitle('SST in 1998',fontsize=20,y=0.95)
plt.subplots_adjust(hspace=0.2)
plt.subplots_adjust(wspace=0.5)
plt.show()
if __name__ == '__main__':
main()