import numpy as np
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs
from netCDF4 import Dataset
from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter
from cnmaps import get_adm_maps,clip_contours_by_map,draw_maps
from cartopy.io.shapereader import Reader
plt.rcParams['font.sans-serif'] = [u'SimHei']
plt.rcParams['axes.unicode_minus'] = False
f = Dataset('air.mon.mean.nc')
air = f.variables['air'][0,:,:]
lat = f.variables['lat'][:]
lon = f.variables['lon'][:]
lon2=lon[210:247]
lat2=lat[129:175]
air2=air[129:175,210:247]
fig=plt.figure(figsize=(16,9))
#001
ax = fig.add_subplot(111,projection=ccrs.PlateCarree())
ax.set_extent([73,140,15,55],crs=ccrs.PlateCarree())
ax.coastlines('50m')
map_polygon = get_adm_maps(country='中华人民共和国', record='first', only_polygon=True)
draw_maps(get_adm_maps(level='省'), linewidth=0.8, color='r')
gl = ax.gridlines(crs = ccrs.PlateCarree(),draw_labels = True,linewidth = 1,alpha = 0.5,color = 'grey',linestyle = '--')
gl.xlocator = plt.FixedLocator(np.arange(70, 140, 10))
gl.ylocator = plt.FixedLocator(np.arange(15, 55, 10))
gl.xlabel_style = {'size': 10}
gl.ylabel_style = {'size':10}
gl.right_labels = False
gl.top_labels = False
con=ax.contourf(lon,lat,air-273.15,levels=np.arange(-40,40,2),cmap='jet',transform=ccrs.PlateCarree())
draw_maps(get_adm_maps(country='中华人民共和国',level='国'), color='k',linewidth=0.8)
clip_contours_by_map(con, map_polygon)
fig.colorbar(con,ax=ax,shrink=0.8,pad=0.05)
ax.set_title('气温(℃)',fontsize=12)
#002
bx = fig.add_axes([0.601, 0.167, 0.2, 0.2], projection=ccrs.PlateCarree())
bx.set_extent([105.25, 123.25, 2, 25],ccrs.PlateCarree())
bx.add_feature(cfeature.COASTLINE)
con2=bx.contourf(lon2,lat2,air2-273.15,levels=np.arange(-40,40,2),cmap='jet',transform=ccrs.PlateCarree())
ac=clip_contours_by_map(con2, map_polygon)
draw_maps(get_adm_maps(level='省'), linewidth=0.8, color='r')
draw_maps(get_adm_maps(country='中华人民共和国',level='国'), color='k',linewidth=0.8)
plt.savefig('全国气温.jpg', bbox_inches='tight', dpi=300)
plt.show()