- 积分
 - 118
 
	- 贡献
 -  
 
	- 精华
 
	- 在线时间
 -  小时
 
	- 注册时间
 - 2017-5-2
 
	- 最后登录
 - 1970-1-1
 
 
 
 
 
 
 | 
	
 
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册 
 
 
 
x
 
 本帖最后由 JOJOweibo 于 2018-2-6 22:01 编辑  
 
Task:1)绘等值线(以SLP为例);2)画出两幅子图 
 
仍需改进的地方:colorbar的大小和方向需要调整;地图调整为函数形式;地图调整为区域地图;叠加等值线(希望有高手回帖指点,谢谢) 
 
参考资料:1)matoplot画等值线 https://matplotlib.org/gallery/images_contours_and_fields/pcolormesh_levels.html 
2)cartopy入门 http://bbs.06climate.com/forum.p ... 3601&extra=page%3D1 
3)官网例子 http://scitools.org.uk/cartopy/docs/latest/matplotlib/advanced_plotting.html 
4)axes与subplot的区别 https://www.zhihu.com/question/51745620 
 - import matplotlib.pyplot as plt
 
 - import cartopy.crs as ccrs
 
 - import cartopy.feature as cfeature
 
 - from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
 
 - from netCDF4 import Dataset 
 
 - from matplotlib.ticker import MaxNLocator
 
 - from matplotlib.colors import BoundaryNorm
 
 - #==============================================================================
 
 - #读入数据
 
 - f = Dataset('...slp.nc')
 
 - #print(f)
 
  
- slp1    = f.variables['slp1'][0,:,:]
 
 - slp2    = f.variables['slp2'][0,:,:]
 
 - lat          = f.variables['lat'][:]
 
 - lon          = f.variables['lon'][:]
 
  
- #==============================================================================
 
 - #创建图纸,子图
 
 - fig = plt.figure(figsize=(8, 10))
 
 - #set colormap
 
 - levels = MaxNLocator(nbins=16).tick_values(-2.0, 2.0) #设置等值线值
 
 - cmap   = plt.get_cmap('bwr')  #选择coloarmap
 
 - norm   = BoundaryNorm(levels, ncolors=cmap.N, clip=True) #标准化coloarmap
 
 - #画第一个子图
 
 - # Label axes of a Plate Carree projection with a central longitude of 180:
 
 - ax1 = plt.subplot(211, projection=ccrs.PlateCarree(central_longitude=180))
 
 - ax1.set_global() #使得轴域(Axes即两条坐标轴围城的区域)适应地图的大小
 
 - ax1.coastlines() #画出海岸线
 
  
- ax1.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree()) #标注坐标轴
 
 - ax1.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
 
 - lon_formatter = LongitudeFormatter(zero_direction_label=True) #zero_direction_label=False 用来设置经度的0度加不加E和W
 
 - lat_formatter = LatitudeFormatter()
 
 - ax1.xaxis.set_major_formatter(lon_formatter)
 
 - ax1.yaxis.set_major_formatter(lat_formatter) #标注坐标轴
 
  
- #画等值线
 
 - plt.contourf(lon,lat,slp1,15, levels=levels, cmap=cmap,transform=ccrs.PlateCarree())
 
  
- plt.colorbar()
 
 - #==============================================================================
 
 - #画第二个子图
 
 - # Label axes of a Mercator projection without degree symbols in the labels
 
 - # and formatting labels to include 1 decimal place:
 
 - ax2 = plt.subplot(212, projection=ccrs.PlateCarree(central_longitude=180))
 
 - ax2.set_global()
 
 - ax2.coastlines()
 
 - ax2.set_xticks([-180, -120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree())
 
 - ax2.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
 
 - lon_formatter = LongitudeFormatter(zero_direction_label=True) #zero_direction_label=False 用来设置经度的0度加不加E和W
 
 - lat_formatter = LatitudeFormatter()
 
 - ax2.xaxis.set_major_formatter(lon_formatter)
 
 - ax2.yaxis.set_major_formatter(lat_formatter)
 
  
- #画等值线
 
 - plt.contourf(lon,lat,slp2,15, levels=levels, cmap=cmap,transform=ccrs.PlateCarree())
 
 - plt.colorbar(ax=ax2)
 
 - fig.tight_layout()
 
 - plt.savefig("./fig/06carotpy.contourf.png") 
 
  复制代码 
 
 
 
 |   
- 
 
 
 
 
 
评分
- 
查看全部评分
 
 
 
 
 
 |