; a% r# P: c# h
文章目录前言一、基础介绍二、区域地图的绘制总结前言' O T' c- A' K
常用地图底图的绘制一般由Basemap或者cartopy模块完成,由于Basemap库是基于python2开发的一个模块,目前已经不开发维护。故简单介绍cartopy模块的一些基础操作。 # p0 e5 E% `- S4 |- h
" Q" w) Y1 L& R3 x! U+ q
如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的python裙,关注小编,并私信“01”即可进裙,领取python学习资料,会节约很多时间,减少很多遇到的难题。
# f3 T/ b- k! F& |. W8 m6 i 一、基础介绍
' Q0 g# M% o0 \! K5 |* c 首先导入相关模块。 import numpy as np
0 c6 z/ j# J- {, d( b9 A+ W import matplotlib.pyplot as plt
: V! k1 T( k. t% E; z import cartopy.crs as ccrs
, t% d7 t N+ x. ?; X& j import cartopy.feature as cfeature
9 Y# x `" {2 J# N% Q& E from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
* V; v# \) L0 X6 l2 r' P 123459 Q" _2 q6 ]9 H$ Q, g8 A
首先介绍参数projection,该命令可以配合ccrs设置投影类型,此处以方形投影命令为示例。其中central_longitude参数为投影中心位置。其中心设置与Basemap设置规则一样,详情可以看上一篇文章。 ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
; l4 {5 `4 ?. s5 [% r, g$ J2 L 1在设置好绘制类型后,绘制地图各特征量。其代码如下: #ax.add_feature(cfeature.LAKES.with_scale(scale))
/ ^' u' ^) k0 M6 I2 Z ax.add_feature(cfeature.OCEAN.with_scale(scale))8 N) T/ B! l' B5 T3 |9 |. u
#ax.add_feature(cfeature.RIVERS.with_scale(scale))1 A7 U( T( R6 T% z% K2 H+ U0 @
#ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)7 F% E) m- c: y8 x% B, v# W& ~, n. z
ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)
! c" E- W4 l% B& |1 q9 w0 l, b 12345
P2 F5 {4 G# K 参数scale为地图分辨率,目前支持10m,50m,110m,参数lw为线条粗细。此处绘制海岸线和海洋,效果图如下: + t6 b: ?' {5 ~ A
( a3 G6 x/ i; }& e' \/ d6 Z$ f
3 O% n1 e/ B$ a4 \7 [3 @
在绘制结束后,作为地图。经纬度自然是必不可少的,在该模块中,引进同时设置坐标轴标签改变该标签刻度的表示,具体形式如下: ax.set_xticks(np.arange(0,361,40), crs=ccrs.PlateCarree())
% u! D G, L( e; I; ? ax.set_yticks(np.arange(-90,90+30,30), crs=ccrs.PlateCarree())6 E" |6 x& k; p4 b c4 M* X
#zero_direction_label用来设置经度的0度加不加E和W0 ^3 Q0 M- H. z# k4 G! z6 p0 t7 I
lon_formatter = LongitudeFormatter(zero_direction_label=False)
% j! ~/ r% l2 N lat_formatter = LatitudeFormatter()
k$ B3 t9 U4 x: @$ F0 ~; | ax.xaxis.set_major_formatter(lon_formatter)$ o( N( H% @/ S w; o
ax.yaxis.set_major_formatter(lat_formatter)
0 [ f; H+ @4 \, n+ v& A4 Z6 e9 n 1234567可以看到效果图如下:
! S! K) z: Y; d/ G$ P
% q( B2 W+ Q& l
' l4 ^; Z$ f- A: s6 n9 W+ e 当然如果想对坐标轴粗细变化可以引入一下命令。 ax.outline_patch.set_visible(False)) x! F8 O- M! j( z# P& m2 N* [
ax.spines[bottom].set_visible(True)
* C: x3 U' K- f7 ^4 V ax.spines[left].set_visible(True)4 E7 h& b. W/ {
ax.spines[right].set_visible(True)
1 p' S; v2 H+ l) B3 X ax.spines[top].set_visible(True) h- Q5 v) K5 ^9 l# X0 ~
ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细
" Z" O3 {! a* B1 a( m ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细* J2 \" M6 r9 d* e# {. l; v3 A
ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细
+ j; l G. h$ G ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细
. W f1 D7 y! d9 a H- K. g w. O) v" N6 ]' e4 H, Z; H
12345678910应该在该模块下,控制坐标轴的命令已经和常规不一样。因此先关闭该控制,然后开启常规坐标轴设置。 9 s6 B4 C7 h( S* l6 N
二、区域地图的绘制( j( A. k" u1 G
当我们在某一小块区域研究时,需要绘制区域地图。此时我们可以引入命令: ax.set_extent(box,crs=ccrs.PlateCarree())
: ~: N4 i, M) \( T8 e( y7 V, A1 i 1
! w, I9 D7 M5 [( a8 G' [4 B 其中box为绘制区域,crs为投影类型。其他命令基本不变。设置box为[40,180,0,90],可得到效果图如下: 4 i% y! ?+ t3 C8 s+ {; b" J
! M: Q1 p1 |8 @/ {
- g9 ?# ~3 k& z; I# ]6 a
总结
& [* Y7 V. K. y/ L2 V' b 为方便各位读者,我书写了绘制地图的函数,大家在使用时可直接调用。此处示例为方形投影,若希望绘制其他投影。只需要修改函数部分参数即可。代码如下: def map_make(scale,box,xstep,ystep):. o& b& Z6 H( S6 j6 k* a
ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))8 E. i9 G- i: C5 B! i; S
a = (box[1]-box[0])//xstep
, G1 ^& f9 ?- i4 m x_start = box[1] - a*xstep2 s" N$ E. L" `6 F2 X( Y
a = (box[3]-box[2])//ystep
, y1 E+ k2 x3 f9 F7 E5 n. y4 I y_start = box[3] - a*ystep, L5 g5 K) L- H" n
ax.set_extent(box,crs=ccrs.PlateCarree())0 ~0 e1 O6 {$ K* p8 ~7 L' W" G2 B
#ax.add_feature(cfeature.LAKES.with_scale(scale))) _4 m# H) Y7 C! ?3 K
#ax.add_feature(cfeature.OCEAN.with_scale(scale))
6 B% n% \1 x& [1 j$ D: {. R7 Z #ax.add_feature(cfeature.RIVERS.with_scale(scale))7 n6 w1 y K8 _$ t5 \5 Y9 j+ k
#ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)% A0 \% a( C- J( i: O6 f% w( O
ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)- m& j9 U! I9 d( U, J
8 i" P j/ W- W( `- J ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree())
% }+ T, q8 M; }2 M) T# U ax.set_yticks(np.arange(y_start,box[3]+ystep,ystep), crs=ccrs.PlateCarree())
6 Y" j" q$ L, c+ y$ q #zero_direction_label用来设置经度的0度加不加E和W
, J* m- ?, |6 |3 R8 z5 o& m lon_formatter = LongitudeFormatter(zero_direction_label=False)8 [0 r& E7 A1 S6 G
lat_formatter = LatitudeFormatter()
3 U1 g* e `( v$ c. W: f [ ax.xaxis.set_major_formatter(lon_formatter)5 i( I; C: I" i
ax.yaxis.set_major_formatter(lat_formatter)+ {- I2 Y- b" T6 a; d% M# r2 Q, N
#添加网格线9 z) N" H' C; M; b7 Q% Q: C }
ax.grid()1 s* e* F! o. e- N9 H, J" k5 |
! ?" Q' [) D& n6 h5 Q ax.outline_patch.set_visible(False)
) O8 J. x$ `% | ax.spines[bottom].set_visible(True)
4 |. w9 _$ F) m# P( I0 u ax.spines[left].set_visible(True)
- w& I- j/ l0 A ax.spines[right].set_visible(True)+ d5 x4 T% L! a2 q6 t' N; p* _
ax.spines[top].set_visible(True)
2 q% j' s! o5 |: l- R& f2 f) F V ax.spines[bottom].set_linewidth(2.5);###设置底部坐标轴的粗细+ H1 f9 E3 N5 H, W: Y, t. e
ax.spines[left].set_linewidth(2.5);####设置左边坐标轴的粗细5 Q+ z) Q9 Q& ?9 U3 {
ax.spines[right].set_linewidth(2.5);###设置右边坐标轴的粗细" S7 y5 q' g7 e* w( x6 A
ax.spines[top].set_linewidth(2.5);####设置上部坐标轴的粗细+ ^% L2 i' ^$ t2 _
. G4 p+ R5 x* X! h& W# Z& F, L
return ax最后多说一句,想学习Python可联系阿喵,这里有我自己整理的整套python学习资料和路线,想要这些资料的都可以关注阿喵,并私信“01”领取。 ) P- d" a' E! K% \
% ^- G% G* Q( ^, u; i
2 V0 M1 Y4 o3 R4 b6 A7 f1 D4 h. U. L2 ~6 N% `
/ Y5 N. v+ I! N) R |