爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 26142|回复: 14

[经验总结] python matplotlib cartopy学习笔记-练习2

[复制链接]

新浪微博达人勋

发表于 2018-2-7 15:19:37 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 JOJOweibo 于 2018-2-7 15:23 编辑

参考资料:
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

其他说明请看代码中的注释,不明白的可以发邮件联系,欢迎一起交流学习
  1. #==============================================================================

  2. #JOJO, IAP, Beiing, Email:mtjsummer@163.com
  3. #2018-02-07

  4. #练习2目标:
  5. #  1.使用函数形式调用cartopy
  6. #  2.绘制2个子图
  7. #  3.调整colorbar
  8. #  4.叠加显著性检验
  9. #  5.添加标题

  10. #==============================================================================

  11. import matplotlib.pyplot as plt
  12. import matplotlib as mpl
  13. import cartopy.crs as ccrs
  14. import cartopy.feature as cfeature
  15. from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
  16. from netCDF4 import Dataset
  17. import numpy as np

  18. #==============================================================================

  19. #读入数据
  20. f = Dataset('...slp.nc')
  21. #print(f)

  22. slp1    = f.variables['slp1'][0,:,:]
  23. slp2 = f.variables['slp2'][0,:,:]
  24. lats         = f.variables['lat'][:]
  25. lons         = f.variables['lon'][:]

  26. #==============================================================================

  27. #创建图纸,以及两个子图
  28. fig = plt.figure(figsize=(8,6))
  29. ax  = fig.add_subplot(211, projection=ccrs.PlateCarree(central_longitude=180))
  30. #定义投影方式
  31. projection = ccrs.PlateCarree()

  32. #==============================================================================

  33. #函数形式,调用cartopy,绘制全球地图
  34. def make_map(ax):
  35.     projection = ccrs.PlateCarree()
  36.     ax.set_global()
  37.     ax.coastlines(linewidth=0.5)
  38.     '''标注坐标轴'''
  39.     ax.set_xticks(np.linspace(-180, 180, 5), crs=projection)
  40.     ax.set_yticks(np.linspace(-90, 90,5), crs=projection)
  41.     '''zero_direction_label=True 有度的标识,False则去掉'''
  42.     lon_formatter = LongitudeFormatter(zero_direction_label=True)
  43.     lat_formatter = LatitudeFormatter()
  44.     ax.xaxis.set_major_formatter(lon_formatter)
  45.     ax.yaxis.set_major_formatter(lat_formatter)
  46.     '''添加网格线'''
  47.     #gl = ax.gridlines()
  48.     #ax.grid()
  49.     return ax

  50. #==============================================================================

  51. #设置colorbar
  52. # Set the colormap and norm to correspond to the data for which the colorbar will be used
  53. # 指定colorbar为bwr
  54. cmap = mpl.cm.RdBu_r
  55. # 设定每个图的colormap和colorbar所表示范围是一样的,归一化
  56. norm = mpl.colors.Normalize(vmin=-2.0, vmax=2.0)
  57. # 设置levels
  58. levels = mpl.ticker.MaxNLocator(nbin=16).tick_values(-2.0, 2.0)

  59. #==============================================================================

  60. # 开始绘图

  61. # 在ax上画地图
  62. ax = make_map(ax)
  63. # 填充颜色
  64. p  = ax.contourf(lons, lats, slp1, levels=levels, cmap=cmap, transform=projection)
  65. # 画等值线
  66. pl = ax.contour(lons, lats, slp1, levels=levels, linewidths=0.5, colors='k', transform=projection)  
  67. # 设置title
  68. ax.set_title('Reg_SLP1')
  69. # 添加colorbar
  70. fig.colorbar(p,ax=ax, extend='both')

  71. # 在ax1上画图
  72. ax1  = fig.add_subplot(212, projection=ccrs.PlateCarree(central_longitude=180))
  73. # 在ax1上画地图
  74. ax1 = make_map(ax1)
  75. # 填充颜色
  76. p1  = ax1.contourf(lons, lats, slp2, levels=levels, cmap=cmap, transform=projection)
  77. # 画等值线
  78. pl1 = ax1.contour(lons, lats, slp2, levels=levels, linewidths=0.5, colors='k', transform=projection)  
  79. # 设置title
  80. ax1.set_title('Reg_SLP2')
  81. # 添加colorbar
  82. fig.colorbar(p1,ax=ax1, extend='both')

  83. #==============================================================================

  84. # 保存图片
  85. # adjust spacing between subplots so ax and ax1 title and ticks labels don't overlay
  86. fig.tight_layout()
  87. plt.savefig("./fig/06carotpy.multimap.png")

复制代码


06carotpy.multimap.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-7 15:33:50 | 显示全部楼层
{:eb502:}{:eb502:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-7 16:01:12 | 显示全部楼层
{:eb502:}{:eb502:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-8 10:15:15 | 显示全部楼层
{:eb502:}{:eb502:}{:eb502:}{:eb502:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-12 09:30:22 | 显示全部楼层
{:eb502:}{:eb502:}{:eb502:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-12 19:56:05 | 显示全部楼层
先分享,后下载,再学习,谢谢楼主{:5_213:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-11-26 08:34:38 | 显示全部楼层
楼主我想请问 第69行的norm作用是什么 因为看到后面没有使用它
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-12-1 11:23:45 | 显示全部楼层
great  i also need  it
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2019-10-3 19:05:39 | 显示全部楼层
感谢!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2019-11-7 10:41:45 | 显示全部楼层
本帖最后由 燃烧的洋葱 于 2019-11-7 10:43 编辑

学习一下cartopy~~~
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表