python Cartopy的基础使用详解

作者:小龙人_tao 时间:2022-03-22 06:41:18 

前言

常用地图底图的绘制一般由Basemap或者cartopy模块完成,由于Basemap库是基于python2开发的一个模块,目前已经不开发维护。故简单介绍cartopy模块的一些基础操作。 一、基础介绍

首先导入相关模块。


import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter

首先介绍参数projection,该命令可以配合ccrs设置投影类型,此处以方形投影命令为示例。其中central_longitude参数为投影中心位置。其中心设置与Basemap设置规则一样,详情可以看上一篇文章。


ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))

在设置好绘制类型后,绘制地图各特征量。其代码如下:


#ax.add_feature(cfeature.LAKES.with_scale(scale))
ax.add_feature(cfeature.OCEAN.with_scale(scale))
#ax.add_feature(cfeature.RIVERS.with_scale(scale))
#ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)
ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)

参数scale为地图分辨率,目前支持10m,50m,110m,参数lw为线条粗细。此处绘制海岸线和海洋,效果图如下:

python Cartopy的基础使用详解

在绘制结束后,作为地图。经纬度自然是必不可少的,在该模块中,引进同时设置坐标轴标签改变该标签刻度的表示,具体形式如下:


ax.set_xticks(np.arange(0,361,40), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(-90,90+30,30), crs=ccrs.PlateCarree())
#zero_direction_label用来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)

可以看到效果图如下:

python Cartopy的基础使用详解

当然如果想对坐标轴粗细变化可以引入一下命令。


ax.outline_patch.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)
ax.spines['right'].set_visible(True)
ax.spines['top'].set_visible(True)
ax.spines['bottom'].set_linewidth(2.5);###设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(2.5);####设置左边坐标轴的粗细
ax.spines['right'].set_linewidth(2.5);###设置右边坐标轴的粗细
ax.spines['top'].set_linewidth(2.5);####设置上部坐标轴的粗细

应该在该模块下,控制坐标轴的命令已经和常规不一样。因此先关闭该控制,然后开启常规坐标轴设置。

二、区域地图的绘制

当我们在某一小块区域研究时,需要绘制区域地图。此时我们可以引入命令:


ax.set_extent(box,crs=ccrs.PlateCarree())

其中box为绘制区域,crs为投影类型。其他命令基本不变。设置box为[40,180,0,90],可得到效果图如下:

python Cartopy的基础使用详解

总结

为方便各位读者,我书写了绘制地图的函数,大家在使用时可直接调用。此处示例为方形投影,若希望绘制其他投影。只需要修改函数部分参数即可。代码如下:


def map_make(scale,box,xstep,ystep):
 ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
 a = (box[1]-box[0])//xstep
 x_start = box[1] - a*xstep
 a = (box[3]-box[2])//ystep
 y_start = box[3] - a*ystep
 ax.set_extent(box,crs=ccrs.PlateCarree())
 #ax.add_feature(cfeature.LAKES.with_scale(scale))
 #ax.add_feature(cfeature.OCEAN.with_scale(scale))
 #ax.add_feature(cfeature.RIVERS.with_scale(scale))
 #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)
 ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)

ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree())
 ax.set_yticks(np.arange(y_start,box[3]+ystep,ystep), crs=ccrs.PlateCarree())
 #zero_direction_label用来设置经度的0度加不加E和W
 lon_formatter = LongitudeFormatter(zero_direction_label=False)
 lat_formatter = LatitudeFormatter()
 ax.xaxis.set_major_formatter(lon_formatter)
 ax.yaxis.set_major_formatter(lat_formatter)
 #添加网格线
 ax.grid()

ax.outline_patch.set_visible(False)
 ax.spines['bottom'].set_visible(True)
 ax.spines['left'].set_visible(True)
 ax.spines['right'].set_visible(True)
 ax.spines['top'].set_visible(True)
 ax.spines['bottom'].set_linewidth(2.5);###设置底部坐标轴的粗细
 ax.spines['left'].set_linewidth(2.5);####设置左边坐标轴的粗细
 ax.spines['right'].set_linewidth(2.5);###设置右边坐标轴的粗细
 ax.spines['top'].set_linewidth(2.5);####设置上部坐标轴的粗细

return ax

来源:https://blog.csdn.net/weixin_49284189/article/details/109376547

标签:python,Cartopy
0
投稿

猜你喜欢

  • Python的10道简单测试题(含答案)

    2021-12-28 03:57:24
  • Python连接Hadoop数据中遇到的各种坑(汇总)

    2023-09-13 20:16:34
  • Python+matplotlib实现简单曲线的绘制

    2023-01-05 17:21:27
  • PHP笛卡尔积实现算法示例

    2023-09-08 19:58:09
  • php使用递归与迭代实现快速排序示例

    2023-11-14 09:46:31
  • python opencv调用笔记本摄像头

    2022-07-19 14:03:34
  • python使用Pandas库提升项目的运行速度过程详解

    2021-07-21 12:42:29
  • Python 如何实时向文件写入数据(附代码)

    2022-11-10 13:40:03
  • python输出电脑上所有的串口名的方法

    2022-06-23 20:56:03
  • Python+OpenCV图片局部区域像素值处理详解

    2023-10-26 12:59:22
  • Hibernate 的原理与配置

    2023-07-20 21:26:52
  • JavaScript 在各个浏览器中执行的耐性

    2009-02-06 15:26:00
  • Python爬虫获取op.gg英雄联盟英雄对位胜率的源码

    2024-01-02 13:03:52
  • Python趣味爬虫之用Python实现智慧校园一键评教

    2022-02-07 17:53:52
  • PyQt5实现界面(页面)跳转的示例代码

    2023-06-21 18:48:43
  • Python三元运算实现方法

    2021-12-27 06:02:52
  • 分析python服务器拒绝服务攻击代码

    2021-07-21 20:47:29
  • 全面了解CSS内置颜色(color)值

    2008-11-19 12:26:00
  • Python dict的常用方法示例代码

    2023-05-17 09:58:15
  • Go语言编程中字符串切割方法小结

    2023-06-16 01:41:24
  • asp之家 网络编程 m.aspxhome.com