' B$ {/ N" _6 k7 G, I# Z3 {4 |# \3 v 文章目录前言一、基础介绍二、区域地图的绘制总结前言
% H- L _* K$ P2 u2 | 常用地图底图的绘制一般由Basemap或者cartopy模块完成,由于Basemap库是基于python2开发的一个模块,目前已经不开发维护。故简单介绍cartopy模块的一些基础操作。 , Q1 o; h1 X' |) n" |6 i
8 ]+ G4 X& X/ S) [6 r
如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的python裙,关注小编,并私信“01”即可进裙,领取python学习资料,会节约很多时间,减少很多遇到的难题。
5 {0 @0 s: U, |& q; t 一、基础介绍
# z! X6 P+ o L. t! q1 ?. P: U1 j9 H 首先导入相关模块。 import numpy as np. |" h+ K3 a$ H! d" ]. x" r
import matplotlib.pyplot as plt1 T9 b# u$ b7 |# X0 Y: R1 B
import cartopy.crs as ccrs
W+ J7 E1 ?+ l/ e0 P import cartopy.feature as cfeature
4 | U. z; H W+ s$ w$ u0 f from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
# t$ R0 E& }0 s$ n+ l 12345
; j I3 ~ D( r' T- s$ k 首先介绍参数projection,该命令可以配合ccrs设置投影类型,此处以方形投影命令为示例。其中central_longitude参数为投影中心位置。其中心设置与Basemap设置规则一样,详情可以看上一篇文章。 ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
( Z& K6 e" ?! N% h 1在设置好绘制类型后,绘制地图各特征量。其代码如下: #ax.add_feature(cfeature.LAKES.with_scale(scale))' n* }' p8 S9 n) I$ c& K6 h+ T+ u
ax.add_feature(cfeature.OCEAN.with_scale(scale))
9 H7 ~- ]: q+ x6 t$ o4 E) w" z& z) D #ax.add_feature(cfeature.RIVERS.with_scale(scale))
$ @. b+ N5 L; R1 {( K #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)4 n8 M4 X d) c( P i/ z" y
ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)
- B h1 M8 |2 p4 Q/ Z/ _ 123458 i, }8 y" T5 i- ^& T- F
参数scale为地图分辨率,目前支持10m,50m,110m,参数lw为线条粗细。此处绘制海岸线和海洋,效果图如下:
K1 K4 B& {8 I. _
' t" [$ ^4 P3 j ! t) g E" d# [2 V+ a7 B$ c0 K
在绘制结束后,作为地图。经纬度自然是必不可少的,在该模块中,引进同时设置坐标轴标签改变该标签刻度的表示,具体形式如下: ax.set_xticks(np.arange(0,361,40), crs=ccrs.PlateCarree())
9 X6 [/ p' g5 N3 C y9 c" q ax.set_yticks(np.arange(-90,90+30,30), crs=ccrs.PlateCarree())
/ v: M8 U% g8 ?. n5 ?7 w #zero_direction_label用来设置经度的0度加不加E和W" s4 k9 T& D) m$ a+ M6 X9 M
lon_formatter = LongitudeFormatter(zero_direction_label=False)4 s8 c6 q2 U5 E$ Y+ G/ U
lat_formatter = LatitudeFormatter()
1 v0 @( n2 a& [" g3 g: p5 | ax.xaxis.set_major_formatter(lon_formatter)9 R- h9 a$ I. L5 `5 _% t
ax.yaxis.set_major_formatter(lat_formatter), _; [$ b7 m }8 T+ m- M( H
1234567可以看到效果图如下:
6 |; |" ]& b$ R1 c4 P) g# ~+ ?
1 i) x) |5 ~& g2 Z - |" W/ i" K" Q8 Z7 W. F+ Y
当然如果想对坐标轴粗细变化可以引入一下命令。 ax.outline_patch.set_visible(False)
9 ~$ P* {0 t2 } ax.spines[bottom].set_visible(True)
+ y, ]" g. _- h& z% f ax.spines[left].set_visible(True)
( Q* }& W. w. F' e ax.spines[right].set_visible(True)
9 O) m* X. j. _4 H: `$ _7 L ax.spines[top].set_visible(True)
$ q: r$ b+ V$ C! n/ U ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细
1 y9 |) e- M# l9 n0 T/ V5 u/ J8 ` ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细4 |5 e }0 L6 ~; V% a# X
ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细
5 _- h4 B7 K0 _ ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细
* W- M8 n+ O9 p$ D: V/ Z" ^: [& H/ I( t. g
12345678910应该在该模块下,控制坐标轴的命令已经和常规不一样。因此先关闭该控制,然后开启常规坐标轴设置。 f1 F5 _ U5 t; s2 }
二、区域地图的绘制
* e! }0 l- l( X1 {5 j2 t' ] 当我们在某一小块区域研究时,需要绘制区域地图。此时我们可以引入命令: ax.set_extent(box,crs=ccrs.PlateCarree())
0 i$ @- S! v( B7 @( y% J 1& }( e3 }" x+ `8 E s. ~9 p5 c3 \
其中box为绘制区域,crs为投影类型。其他命令基本不变。设置box为[40,180,0,90],可得到效果图如下: / v6 L6 F$ z4 q: k1 O* \, d. ~
3 Y4 v7 X% f6 l( G+ c; ^
3 ~+ V) h2 @$ j9 A) B4 ?- H 总结8 h: t/ `0 V; k( |2 K$ ]" [$ z5 o
为方便各位读者,我书写了绘制地图的函数,大家在使用时可直接调用。此处示例为方形投影,若希望绘制其他投影。只需要修改函数部分参数即可。代码如下: def map_make(scale,box,xstep,ystep):0 U# r# {) l6 I' @6 e, y' D. K
ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180)). w$ K1 q/ a1 h# @8 E+ `: l; H
a = (box[1]-box[0])//xstep# Z( {) r( ^# g8 {+ D* _: \
x_start = box[1] - a*xstep
' B/ u- c" A6 b a = (box[3]-box[2])//ystep
3 |+ T z# h% d) S y_start = box[3] - a*ystep" a# ^ I: Z; F- X+ B- ^ k
ax.set_extent(box,crs=ccrs.PlateCarree())5 |4 \# A6 S: o3 m/ i' r7 D1 Z
#ax.add_feature(cfeature.LAKES.with_scale(scale))6 i8 n- g( b7 G
#ax.add_feature(cfeature.OCEAN.with_scale(scale))1 a9 L4 {# n" c
#ax.add_feature(cfeature.RIVERS.with_scale(scale))
/ h8 U9 l5 V9 L8 v2 Z$ G #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)
- l* H5 J5 W! x1 }7 t& U# |& l ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)" k! M3 N5 w: O, F# n8 u4 b4 @ B; Z
6 T4 u3 h; u1 C8 T8 p9 L ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree())
$ O: ]7 |: L- J9 `! c- N$ {5 Q ax.set_yticks(np.arange(y_start,box[3]+ystep,ystep), crs=ccrs.PlateCarree())+ i) _: B2 S9 [
#zero_direction_label用来设置经度的0度加不加E和W' ?, X: b' _) _6 ?9 i- K, t' ]& p
lon_formatter = LongitudeFormatter(zero_direction_label=False)
: g O: f9 s9 h lat_formatter = LatitudeFormatter()& V5 @1 p y: Y( J( N
ax.xaxis.set_major_formatter(lon_formatter)4 a: |" ~3 N! t3 j6 x8 n5 C
ax.yaxis.set_major_formatter(lat_formatter)* w: h& m, n% t) ?- Y+ M3 R
#添加网格线
. C, Z4 z1 X$ `/ a9 ^" h4 M ax.grid()8 A& l3 T; o$ |# v% B
1 ]* Q0 a9 y+ O. r+ j7 r( i, [, l% i- H2 u ax.outline_patch.set_visible(False)
) L. f8 f& q3 n0 d% f5 _- A' x ax.spines[bottom].set_visible(True)
, l; }% b: F8 {+ r5 i& S ax.spines[left].set_visible(True)
% J! t% O, R1 R+ |; r+ ?% V ax.spines[right].set_visible(True)
: Z- u; o; t8 B/ ], n- k2 ~ ax.spines[top].set_visible(True)4 n/ B+ L. ^' \4 I! g# W' U3 H
ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细4 l- M, D' |8 ]% _
ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细/ h W" w1 p s
ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细
3 G1 v9 m: c9 ` ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细
; w5 T0 @; [# x- C" a" c* } l3 B* f! I1 c5 G$ o2 I7 }9 U; a$ _ ?
return ax最后多说一句,想学习Python可联系阿喵,这里有我自己整理的整套python学习资料和路线,想要这些资料的都可以关注阿喵,并私信“01”领取。 9 o2 T* }' K+ d- i8 p/ Q
- x' P ]! t; ~; w
% x# K. m% U, k7 c3 ]/ y
* B; I! x2 ^3 S- K( S' f; e6 k! x S( s
|