python的基础用法_python Cartopy的基础使用详解

前言

常用地图底图的绘制一般由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为线条粗细。此处绘制海岸线和海洋,效果图如下:

560b1360f85f93cd719df25551fa9a1e.png

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

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)

可以看到效果图如下:

f9576fccd41e39b7f19c13d9fd687fbe.png

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

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],可得到效果图如下:

80384993c3eea858b6e8b43aecdbed45.png

总结

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

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

到此这篇关于python cartopy的基础使用详解的文章就介绍到这了,更多相关python cartopy内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!

希望与广大网友互动??

点此进行留言吧!

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值