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

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

[复制链接]
- N% X) F' T: c

文章目录

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

前言

9 W! X0 U0 E$ o; h1 t

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

b8 H; E) }" U* ^! B
/ c6 V' r; }$ x. l

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

) r# v* w6 X% e* R

一、基础介绍

4 u, c: n7 `5 ^- M. s0 N" Y: `

首先导入相关模块。

import numpy as np / ?( ?7 L" S0 }# v import matplotlib.pyplot as plt4 Z G& M! o6 u! c7 k import cartopy.crs as ccrs t E; K: t% g, q# g) X! q import cartopy.feature as cfeature3 B) l% |) p, U4 H from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter) x) x$ W* F* a' Y 12345 1 m" Z" u0 I5 c$ B

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

ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))5 d5 Z1 o' @4 s+ e5 x/ ? 1

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

#ax.add_feature(cfeature.LAKES.with_scale(scale)) " X5 w' i" W1 q ax.add_feature(cfeature.OCEAN.with_scale(scale)) % s y% i# s- U8 e( P4 k$ O #ax.add_feature(cfeature.RIVERS.with_scale(scale)) ' @2 t: d: @0 D+ ~7 @( C+ _ y3 |4 y #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5) & A& B5 J: q9 F" D0 K ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2) ' _4 [6 K1 w: L2 \# A+ w1 F/ O 12345 & |( T# i' `7 r% c

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

- Y8 u& B0 {% |0 v, e ' R9 A( [- g" s
/ r# n6 y* E R5 Q' ~% ^

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

ax.set_xticks(np.arange(0,361,40), crs=ccrs.PlateCarree()). e( s# _$ A: P1 M' _& ~ ax.set_yticks(np.arange(-90,90+30,30), crs=ccrs.PlateCarree()), ]1 j6 q7 X# i5 F& k( n; T #zero_direction_label用来设置经度的0度加不加E和W; i2 q8 z* V7 J& w) G5 P: { lon_formatter = LongitudeFormatter(zero_direction_label=False)* o, C" q: i( @4 v, | G6 L, ~ lat_formatter = LatitudeFormatter()2 l6 r4 p% Q! K0 R( W- }& `7 h* g ax.xaxis.set_major_formatter(lon_formatter) % X! o" M( A% g. b0 b ax.yaxis.set_major_formatter(lat_formatter) ~' g0 q$ \) [) g! @+ `+ z) ?* M 1234567

可以看到效果图如下:

2 T2 h' o) a9 h( z7 _9 ]( O " g" m3 T9 x# L
4 V9 z8 ?5 k( q% H5 b

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

ax.outline_patch.set_visible(False)8 D" g h0 R( v: x# Z$ s1 c ax.spines[bottom].set_visible(True) 2 L3 z! j) _2 Y' i6 U7 Q ax.spines[left].set_visible(True) 5 V0 r; }' Y$ C" w* d( y ax.spines[right].set_visible(True)8 b; J Z3 d6 I( k. P/ {% `) D ax.spines[top].set_visible(True) ) q2 R5 U4 k$ H% D# M# i ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细 0 c4 O- |7 c, d0 B& R ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细2 C$ {8 H8 \. p ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细/ T, I0 f0 a) r3 U( C4 f6 x8 u ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细% q7 J) m5 A5 b+ B7 y% t 3 D5 }0 s2 O: G* C 12345678910

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

" m. S: E9 Q6 w7 t, l9 {& a

二、区域地图的绘制

* |6 o9 D9 e" O- \) v7 u

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

ax.set_extent(box,crs=ccrs.PlateCarree())% S3 K! S L! K9 E) Z8 z6 t) ^) Q 14 w: z6 Y8 t$ ^% ~* m

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

& q# ~- x# Q8 Q1 }9 N 2 w% ~' I( S' G4 o" J; t
6 |2 S" L+ T7 A

总结

; y# n o- n7 }" u

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

def map_make(scale,box,xstep,ystep):9 A0 ^0 a! y: x( [ ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))2 n* Q2 A3 p7 {) Q* l a = (box[1]-box[0])//xstep ( L. G* Z' Q+ `7 K5 h x_start = box[1] - a*xstep! `3 H$ C$ _5 I a = (box[3]-box[2])//ystep 7 b* k6 v9 `- O: | y_start = box[3] - a*ystep 3 D& f+ x% K, C& \ ax.set_extent(box,crs=ccrs.PlateCarree()), n/ t& j) F" x1 J; R) \8 l #ax.add_feature(cfeature.LAKES.with_scale(scale))+ y0 |( h4 o4 _* d8 N6 ? #ax.add_feature(cfeature.OCEAN.with_scale(scale))# b# M Y# @) C' [; Q #ax.add_feature(cfeature.RIVERS.with_scale(scale))- b5 L$ d! b/ H4 U) h3 e #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5) . z- p3 `1 M* v/ S9 ~ ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2) 0 R5 v4 z C3 }, X( b X: w4 }! {! ]; a3 B2 m ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree())/ t ~. v& _- d) R1 n6 N ax.set_yticks(np.arange(y_start,box[3]+ystep,ystep), crs=ccrs.PlateCarree())& ]. `7 ~; q5 b. h- A+ O #zero_direction_label用来设置经度的0度加不加E和W 7 ~/ Q, }3 r/ b lon_formatter = LongitudeFormatter(zero_direction_label=False)' R& J9 D/ [ R* ]* n! m lat_formatter = LatitudeFormatter()) A! R' w4 z. v6 V" w# F- h ax.xaxis.set_major_formatter(lon_formatter) G7 Q+ P" Y" j, T9 ~ ax.yaxis.set_major_formatter(lat_formatter)+ \* {- b* a5 g" e" h7 x8 ?, j #添加网格线0 N' e; m+ ^& O# f. [$ n& V0 ^" U ax.grid() * _# p. g0 ?7 u, P% N! R" i 6 t2 E% s+ [$ s& [2 y- D2 ` ax.outline_patch.set_visible(False) / A; R% e3 Q: w! Z; L3 [ ax.spines[bottom].set_visible(True) 9 E! A# V. V9 u. {/ z ax.spines[left].set_visible(True) 9 p! {1 @3 V' S2 s6 F8 r, { ax.spines[right].set_visible(True) 3 N7 r+ d. j' v+ } ax.spines[top].set_visible(True). r0 K0 j t1 m+ w" t ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细 " ~# g: d& [, g @ ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细 $ C$ C8 U- ?1 u" S- U; }( j ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细6 `9 L+ L- W! ~) a$ o& T8 r) S8 B ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细 , M8 J& n0 D1 P 6 P1 r; x" ^3 W return ax

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

Z7 Y) }9 i9 L6 P' D8 u ) C! p4 V s+ E9 | ) H$ z" x9 N! H3 b! u# Y- _3 O . H1 P- D/ q. X' l2 s , X$ |) i6 k/ C
回复

举报 使用道具

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