注解
Click here to download the full example code
绘制符号¶
利用 PyGMT 的 pygmt.Figure.plot()
可以绘制多种常见的符号类型。
绘制简单符号¶
首先利用 numpy.random
模块生成数据集:
Out:
x:
[ 183.7086107 235.56428758 215.87945476 203.87926358 164.04167764
164.03950683 155.2275251 227.95585312 204.10035106 213.726532
151.85260449 237.29188669 224.91983767 169.11051996 166.36424705
166.50640589 177.38180187 197.22807885 188.87505168 176.21062262]
y:
[ 32.82970263 -0.23542975 10.4501254 15.64532903 21.9248989
44.9623173 3.97716475 25.99641069 31.46901982 -6.74847111
32.52813963 1.93668866 -5.44638849 56.42198761 57.59424232
46.58781437 11.32296384 -3.16295202 37.89631186 20.81067456]
z:
[ 1.48815294 2.98070764 1.13755408 4.63728161 2.03511993 3.65008914
2.2468443 3.08027208 3.18684112 1.73941782 4.87833851 4.10053129
4.75799577 4.5793094 3.39159992 4.68749694 1.35397001 1.78393145
1.18090916 2.30132132]
通过 pygmt.Figure.plot()
绘制符号时需要用 style
选项控制符号类型和大小:

Out:
颜色变化的符号¶
如果想要让符号的颜色根据某个数值的不同而使用不同的颜色,则需要使用 color
选项。color
选项表示符号的填充色由某个数值以及 CPT 颜色表所控制。CPT 颜色表给出了数值与颜色之间的对应关系。因而对于任意一个符号,我们都可以给其一个数值,PyGMT 会根据该数值从 CPT 颜色表中找到对应的颜色作为该符号的填充色。因而,在输入数据中,需要在 X 和 Y 坐标的基础上额外加一列 Z 值,用于控制符号的填充色。
下面的示例中,首先使用 pygmt.makecpt()
模块,以内置 CPT 颜色表 hot 为基础,生成了一个新的 CPT 颜色表,该 CPT 颜色表为 1 到 5 之内的每个数值都对应了一个颜色。在使用 pygmt.Figure.plot()
绘制符号时,通过 color
选项决定每个符号的填充颜色:

Out:
具有透明度的符号¶
pygmt.Figure.plot()
的 transparency
选项可以为符号添加透明度。
首先利用 numpy
模块的 numpy.arange()
函数和 numpy.ones()
函数生成示例数据:
Out:

Out:
绘制不同的符号¶
向 pygmt.Figure.plot()
的 style
选项传递不同的参数可以绘制多种类型的符号。
e:椭圆
[[lon, lat, direction, major_axis, minor_axis]]
j:旋转的矩形
[[lon, lat, direction, width, height]]
r:矩形
[[lon, lat, width, height]]
R:圆角矩形
[[lon, lat, width, height, radius]]
w:饼形楔形
[[lon, lat, radius, startdir, stopdir]]
最后两个参数是从水平逆时针方向给出的方向
E、J 和 W 与 e、j 和 w 相似,但是期望输入的是地理方位角和距离。
import numpy as np
import pygmt
fig = pygmt.Figure()
fig.basemap(region=[0, 6, 0, 2], projection="x3c", frame=True)
# 椭圆
data = np.array([[0.5, 1, 45, 3, 1]])
fig.plot(data=data, style="e", color="orange", pen="2p,black")
# 旋转的矩形
data = np.array([[1.5, 1, 120, 5, 0.5]])
fig.plot(data=data, style="j", color="red3", pen="2p,black")
# 矩形
data = np.array([[3, 1, 4, 1.5]])
fig.plot(data=data, style="r", color="dodgerblue", pen="2p,black")
# 圆角矩形
data = np.array([[4.5, 1, 1.25, 4, 0.5]])
fig.plot(data=data, style="R", color="seagreen", pen="2p,black")
# 饼形楔形
data = np.array([[5.5, 1, 2.5, 45, 330]])
fig.plot(data=data, style="w", color="lightgray", pen="2p,black")
fig.show()

Out:
示例:绘制震中分布图¶
通过 PyGMT 提供的 pygmt.datasets
包可以访问 GMT 服务器上的示例数据。第一次访问时,数据会自动下载并保存到 GMT 的缓存目录中(通常为 ~/.gmt/cache
)。
通过 pygmt.datasets.load_japan_quakes()
函数加载日本附近海啸诱发地震,数据以 pandas.DataFrame
形式加载。
Out:
gmtwhich [NOTICE]: -> Download cache file: @tut_quakes.ngdc
[131.28999999999999, 150.88999999999999, 34.020000000000003, 50.770000000000003]
year month day latitude longitude depth_km magnitude
0 1987 1 4 49.77 149.29 489 4.1
1 1987 1 9 39.90 141.68 67 6.8
2 1987 1 9 39.82 141.64 84 4.0
3 1987 1 14 42.56 142.85 102 6.5
4 1987 1 16 42.79 145.10 54 5.1
利用 pygmt.Figure.plot()
方法绘制震中分布:

Out:
其中,style="c0.3c"
表示将数据点绘制成半径为 0.3 厘米的圆,
color="white"
和 pen="1p,black"
分别控制了符号的填充样式和边框样式。
通过 sizes
参数可以用圆的大小来表征地震震级,这里通过幂运算可以更好的表现震级差异:

Out:
请注意,上例中 style="cc"
并没有指明符号大小,仅包含符号 c
(圆形)和单位 c
(厘米)。
那么如何用符号的填充颜色来表征发震深度呢?简单来说,首先根据发震深度利用 pygmt.makecpt()
创建一个连续的颜色表,然后在 pygmt.Figure.plot()
中设置 cmap=True
使用颜色映射即可。
fig = pygmt.Figure()
fig.basemap(region=region, projection="M15c", frame=True)
fig.coast(land="black", water="skyblue")
# 将 data.depth_km 的最小值和最大值赋予 series 指定 cpt 数值范围
# 使用 matplotlib 的 viridis 颜色表
pygmt.makecpt(cmap="viridis", series=[data.depth_km.min(), data.depth_km.max()])
fig.plot(
x=data.longitude,
y=data.latitude,
sizes=0.02 * 2 ** data.magnitude,
color=data.depth_km, # 发震深度决定填充颜色
cmap=True, # 设置为 True 表示使用 makecpt 制作的 cpt 作为颜色表
style="cc",
pen="1p,black",
)
# 绘制 colorbar
fig.colorbar(frame='af+l"Depth (km)"')
fig.show()

Out:
Total running time of the script: ( 0 minutes 11.146 seconds)