| 
 
	积分118贡献 精华在线时间 小时注册时间2017-5-2最后登录1970-1-1 
 | 
 
| 
本帖最后由 JOJOweibo 于 2018-2-7 15:23 编辑
x
登录后查看更多精彩内容~您需要 登录 才可以下载或查看,没有帐号?立即注册 
  
 参考资料:
 1.调整字体,colorbar, colormap, 刻度 https://www.cnblogs.com/qqhfeng/p/5567539.html
 2. cartopy 官网例子 http://scitools.org.uk/cartopy/d ... xes_grid_basic.html
 3. matplotlib 上面关于colorbar的例子 https://matplotlib.org/examples/api/colorbar_basics.html
 https://matplotlib.org/examples/api/colorbar_basics.html
 4. http://bbs.06climate.com/forum.p ... 3601&extra=page%3D1
 注意这帖子里面的第二步程序会自动完成,地图数据自动导入,无需手动设置
 地图数据文件路径:.../.local/share/cartopy/shapefiles/natural_earth/physical
 
 其他说明请看代码中的注释,不明白的可以发邮件联系,欢迎一起交流学习
 
 复制代码#==============================================================================
#JOJO, IAP, Beiing, Email:mtjsummer@163.com
#2018-02-07
#练习2目标:
#  1.使用函数形式调用cartopy
#  2.绘制2个子图
#  3.调整colorbar
#  4.叠加显著性检验
#  5.添加标题
#==============================================================================
import matplotlib.pyplot as plt
import matplotlib as mpl
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from netCDF4 import Dataset 
import numpy as np
#==============================================================================
#读入数据
f = Dataset('...slp.nc')
#print(f)
slp1    = f.variables['slp1'][0,:,:]
slp2 = f.variables['slp2'][0,:,:]
lats         = f.variables['lat'][:]
lons         = f.variables['lon'][:]
#==============================================================================
#创建图纸,以及两个子图
fig = plt.figure(figsize=(8,6))
ax  = fig.add_subplot(211, projection=ccrs.PlateCarree(central_longitude=180))
#定义投影方式
projection = ccrs.PlateCarree()
#==============================================================================
#函数形式,调用cartopy,绘制全球地图
def make_map(ax):
    projection = ccrs.PlateCarree()
    ax.set_global()
    ax.coastlines(linewidth=0.5)
    '''标注坐标轴'''
    ax.set_xticks(np.linspace(-180, 180, 5), crs=projection) 
    ax.set_yticks(np.linspace(-90, 90,5), crs=projection)
    '''zero_direction_label=True 有度的标识,False则去掉'''
    lon_formatter = LongitudeFormatter(zero_direction_label=True) 
    lat_formatter = LatitudeFormatter()
    ax.xaxis.set_major_formatter(lon_formatter)
    ax.yaxis.set_major_formatter(lat_formatter)
    '''添加网格线'''
    #gl = ax.gridlines()
    #ax.grid()
    return ax
#==============================================================================
#设置colorbar
# Set the colormap and norm to correspond to the data for which the colorbar will be used
# 指定colorbar为bwr
cmap = mpl.cm.RdBu_r
# 设定每个图的colormap和colorbar所表示范围是一样的,归一化
norm = mpl.colors.Normalize(vmin=-2.0, vmax=2.0)
# 设置levels
levels = mpl.ticker.MaxNLocator(nbin=16).tick_values(-2.0, 2.0)
#==============================================================================
# 开始绘图
# 在ax上画地图
ax = make_map(ax)
# 填充颜色
p  = ax.contourf(lons, lats, slp1, levels=levels, cmap=cmap, transform=projection)
# 画等值线
pl = ax.contour(lons, lats, slp1, levels=levels, linewidths=0.5, colors='k', transform=projection)  
# 设置title
ax.set_title('Reg_SLP1')
# 添加colorbar
fig.colorbar(p,ax=ax, extend='both')
# 在ax1上画图
ax1  = fig.add_subplot(212, projection=ccrs.PlateCarree(central_longitude=180))
# 在ax1上画地图
ax1 = make_map(ax1)
# 填充颜色
p1  = ax1.contourf(lons, lats, slp2, levels=levels, cmap=cmap, transform=projection)
# 画等值线
pl1 = ax1.contour(lons, lats, slp2, levels=levels, linewidths=0.5, colors='k', transform=projection)  
# 设置title
ax1.set_title('Reg_SLP2')
# 添加colorbar
fig.colorbar(p1,ax=ax1, extend='both')
#==============================================================================
# 保存图片
# adjust spacing between subplots so ax and ax1 title and ticks labels don't overlay
fig.tight_layout()
plt.savefig("./fig/06carotpy.multimap.png")
 
 | 
 
  |