收藏本站 劰载中...网站公告 | 吾爱海洋论坛交流QQ群:835383472

零基础小白python入门必看之Cartopy的基础使用

[复制链接]
3 I4 |2 n4 D4 y' A" z6 q

文章目录

前言一、基础介绍二、区域地图的绘制总结

前言

6 {' {5 P1 ^3 w' q

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

5 N7 d/ N7 g) K+ X
3 p) ^* E2 e( r) \* M0 l

如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的python,关注小编,并私信“01”即可进裙,领取python学习资料,会节约很多时间,减少很多遇到的难题。

+ ?+ _) h. w s" `, F" s

一、基础介绍

& C( E& U- Q8 O2 H+ x

首先导入相关模块。

import numpy as np+ ^8 C t* T1 H$ d/ h import matplotlib.pyplot as plt 0 i2 L7 |: r# w/ i, o- a import cartopy.crs as ccrs. j7 J1 b) C0 X& g1 e. b0 L: g& K import cartopy.feature as cfeature ; L% L4 q7 J& h% I from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter 3 P+ d3 R6 ]: j 123454 c6 G5 b7 ^1 D5 n

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

ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0)) 3 V! }+ G. u5 D, m1 ]+ _! u5 d 1

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

#ax.add_feature(cfeature.LAKES.with_scale(scale)) & y) K- p2 k: q3 ^# C) ^( @ ax.add_feature(cfeature.OCEAN.with_scale(scale))4 g! _$ O% @' n: S; p& H# E# J #ax.add_feature(cfeature.RIVERS.with_scale(scale))8 c' v, o: g7 i p. H. \. c #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5) 9 ]3 ~% F1 a l ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)4 g# z/ H$ Z% W! G& ^ 123459 s0 z% Y) Q8 d

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

! I6 |' S; g* }% m0 W0 r7 L 9 ~) y3 C+ v# `1 v$ |2 ~
! F. U/ q2 x- n1 S4 c( Z! }8 B

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

ax.set_xticks(np.arange(0,361,40), crs=ccrs.PlateCarree()) ' u. x* f2 [/ [0 N6 U ax.set_yticks(np.arange(-90,90+30,30), crs=ccrs.PlateCarree())3 U) ?; G S2 [2 v- R) [; M #zero_direction_label用来设置经度的0度加不加E和W6 N1 {" f d6 |! t' e% d( g* _ lon_formatter = LongitudeFormatter(zero_direction_label=False) 4 N, n( p1 y2 O% J, S- c7 U lat_formatter = LatitudeFormatter() S5 ]8 ?2 S1 K# b$ w ax.xaxis.set_major_formatter(lon_formatter) y1 b6 W, @; E2 k$ O: [ ax.yaxis.set_major_formatter(lat_formatter)$ j7 S5 H' t% f! ?. A) b6 } 1234567

可以看到效果图如下:

% x, Y0 z3 z1 R+ t * @# x6 j6 J! h
# P! f9 ?- W6 q+ X

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

ax.outline_patch.set_visible(False)8 x+ G s$ M- U8 G7 ~2 d0 V ax.spines[bottom].set_visible(True) 8 }! G$ v, O' z q2 V* d ax.spines[left].set_visible(True) 0 I8 n% x7 D C$ e; ~ ax.spines[right].set_visible(True)& F* @) x% ]( w9 b/ ?* V ax.spines[top].set_visible(True) 3 x4 {! S7 A- z6 H7 s. u ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细1 P% O3 r8 {) F: X( L ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细 & a. ?: G; a. M' s* f: X ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细 2 [& L+ a( D4 n5 k: s, d/ T ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细 # B: S5 B1 G( N6 F* ^- S; g4 B ! V# [( h5 Y# j& w/ g4 L3 a3 r 12345678910

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

, _2 w: x" W) E+ L ]7 c) c) H

二、区域地图的绘制

1 k) s! i6 d) a% Z6 m

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

ax.set_extent(box,crs=ccrs.PlateCarree())' B" l& `' a! g" S$ t 1 q1 X' {# e7 M+ L

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

* N0 b+ ]: |6 ?' f) | # e- F: I7 r2 R9 t% S' q
5 x) C5 x0 s* Y }" \' q' a2 s

总结

, N9 t2 H( l/ k5 a4 ~

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

def map_make(scale,box,xstep,ystep): 5 i5 G7 ]$ f: m- ^- f ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))' x1 q, ]7 X( h! H+ R- b N a = (box[1]-box[0])//xstep1 V L# Y, _6 {$ E! Z+ F3 P x_start = box[1] - a*xstep 1 `5 T4 T$ a; } a = (box[3]-box[2])//ystep2 i% d; Y$ N* e6 Q7 O# P5 s y_start = box[3] - a*ystep, h: \2 O8 r) q( X$ ^ ax.set_extent(box,crs=ccrs.PlateCarree()) $ v) D k$ s4 f& d8 i2 l# j5 W #ax.add_feature(cfeature.LAKES.with_scale(scale))1 K# D7 V1 l5 ]9 y9 ]7 }! \ #ax.add_feature(cfeature.OCEAN.with_scale(scale))- u( }2 N0 W' F: A #ax.add_feature(cfeature.RIVERS.with_scale(scale)) 4 l3 G% t4 ~9 B$ \- @+ ]. | #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)# C, n* g, N) `5 n ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2). ^$ H; \/ O/ d8 l$ h$ b' Z) a r - F# y% U: {) r1 u4 i5 Z; ^8 _ ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree()) 7 c, {0 `7 }4 |* u! G2 B ax.set_yticks(np.arange(y_start,box[3]+ystep,ystep), crs=ccrs.PlateCarree()) l, K4 C! r% @7 H! d3 j" g #zero_direction_label用来设置经度的0度加不加E和W $ `* X) z( _, ~0 p B lon_formatter = LongitudeFormatter(zero_direction_label=False) & g, [4 ?& @3 y8 g lat_formatter = LatitudeFormatter()# }& q! W; s8 J) K/ E2 b' @1 r) d ax.xaxis.set_major_formatter(lon_formatter)+ k$ {( [$ S5 }3 q: m; j: U ax.yaxis.set_major_formatter(lat_formatter) ( |7 W, b3 m- h# o5 {" O# d) Q, X #添加网格线- @1 {3 Q( K3 @8 h2 q3 L8 k ax.grid() ; x M1 Q2 m; a' a% @) c, r; ~. U) C" ]/ b2 C7 Q ax.outline_patch.set_visible(False) 3 [) j6 R; z, J6 Z6 F1 u5 e ax.spines[bottom].set_visible(True)& H+ p8 z5 X# a* U! u0 b7 F ax.spines[left].set_visible(True) - R% v* H& g0 | ax.spines[right].set_visible(True) & D5 q$ ?7 Y7 E2 \. r: x ax.spines[top].set_visible(True)0 Z1 p- t1 @7 o2 S% W2 G D ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细7 b4 v# x9 }; k/ ~* i6 I ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细 0 f' G7 f- S; g; }7 W2 T- j4 f6 q# z1 x ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细 * C! b% U4 U' m/ c ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细 9 h* M e2 O4 W8 n) Z/ Y {+ G 9 c1 O0 m. F4 D6 R& M* X/ M0 t return ax

最后多说一句,想学习Python可联系阿喵,这里有我自己整理的整套python学习资料和路线,想要这些资料的都可以关注阿喵,并私信“01”领取。

" q4 }; A& z* {5 @ 3 ]8 [* Y7 Y6 ?& C, S 4 n# x; N4 R4 ~3 \3 e" C: B / }4 V6 E4 s0 M8 m3 p1 u# i) t9 l" e1 }+ g. h
回复

举报 使用道具

全部回帖
暂无回帖,快来参与回复吧
懒得打字?点击右侧快捷回复 【吾爱海洋论坛发文有奖】
您需要登录后才可以回帖 登录 | 立即注册
大狼
活跃在2026-3-25
快速回复 返回顶部 返回列表