爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 24151|回复: 11

[源代码] cartopy学习笔记-练习1

[复制链接]

新浪微博达人勋

发表于 2018-2-6 21:58:44 | 显示全部楼层 |阅读模式

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

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

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

  1. import matplotlib.pyplot as plt
  2. import cartopy.crs as ccrs
  3. import cartopy.feature as cfeature
  4. from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
  5. from netCDF4 import Dataset
  6. from matplotlib.ticker import MaxNLocator
  7. from matplotlib.colors import BoundaryNorm
  8. #==============================================================================
  9. #读入数据
  10. f = Dataset('...slp.nc')
  11. #print(f)

  12. slp1    = f.variables['slp1'][0,:,:]
  13. slp2    = f.variables['slp2'][0,:,:]
  14. lat          = f.variables['lat'][:]
  15. lon          = f.variables['lon'][:]

  16. #==============================================================================
  17. #创建图纸,子图
  18. fig = plt.figure(figsize=(8, 10))
  19. #set colormap
  20. levels = MaxNLocator(nbins=16).tick_values(-2.0, 2.0) #设置等值线值
  21. cmap   = plt.get_cmap('bwr')  #选择coloarmap
  22. norm   = BoundaryNorm(levels, ncolors=cmap.N, clip=True) #标准化coloarmap
  23. #画第一个子图
  24. # Label axes of a Plate Carree projection with a central longitude of 180:
  25. ax1 = plt.subplot(211, projection=ccrs.PlateCarree(central_longitude=180))
  26. ax1.set_global() #使得轴域(Axes即两条坐标轴围城的区域)适应地图的大小
  27. ax1.coastlines() #画出海岸线

  28. ax1.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree()) #标注坐标轴
  29. ax1.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
  30. lon_formatter = LongitudeFormatter(zero_direction_label=True) #zero_direction_label=False 用来设置经度的0度加不加E和W
  31. lat_formatter = LatitudeFormatter()
  32. ax1.xaxis.set_major_formatter(lon_formatter)
  33. ax1.yaxis.set_major_formatter(lat_formatter) #标注坐标轴

  34. #画等值线
  35. plt.contourf(lon,lat,slp1,15, levels=levels, cmap=cmap,transform=ccrs.PlateCarree())

  36. plt.colorbar()
  37. #==============================================================================
  38. #画第二个子图
  39. # Label axes of a Mercator projection without degree symbols in the labels
  40. # and formatting labels to include 1 decimal place:
  41. ax2 = plt.subplot(212, projection=ccrs.PlateCarree(central_longitude=180))
  42. ax2.set_global()
  43. ax2.coastlines()
  44. ax2.set_xticks([-180, -120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree())
  45. ax2.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
  46. lon_formatter = LongitudeFormatter(zero_direction_label=True) #zero_direction_label=False 用来设置经度的0度加不加E和W
  47. lat_formatter = LatitudeFormatter()
  48. ax2.xaxis.set_major_formatter(lon_formatter)
  49. ax2.yaxis.set_major_formatter(lat_formatter)

  50. #画等值线
  51. plt.contourf(lon,lat,slp2,15, levels=levels, cmap=cmap,transform=ccrs.PlateCarree())
  52. plt.colorbar(ax=ax2)
  53. fig.tight_layout()
  54. plt.savefig("./fig/06carotpy.contourf.png")
复制代码




06carotpy.contourf.png

评分

参与人数 1金钱 +20 贡献 +5 收起 理由
mofangbao + 20 + 5

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-6 23:23:07 | 显示全部楼层


感谢分享~

临过年都这么努力,一定进步大大滴有~~
{:5_235:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-6 23:31:42 | 显示全部楼层
楼主好棒!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2018-2-7 14:00:34 | 显示全部楼层
很不错,标记一下留着以后用
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-9 10:55:19 | 显示全部楼层
楼主 你太厉害了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-11 10:59:25 | 显示全部楼层
楼主好强大!向你学习!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-2-12 13:49:06 | 显示全部楼层
不错不错!!!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2018-6-21 06:05:53 | 显示全部楼层
Very Good ~ & ~ Thanks~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-12-16 20:16:05 来自手机 | 显示全部楼层
向楼主学习一下ヾ()~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2019-3-24 12:17:16 | 显示全部楼层
楼主好棒!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

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

本版积分规则

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

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

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