0

我想用 pygmt 创建一张地图,我在其中显示 4 次巡航的轨迹。它们确实部分重叠,我对一次巡航特别感兴趣,因此我喜欢把它放在首位。然而,传说应该按日期排序,但所需的游轮不是最近的。pygmt 中是否有一种方法可以重新排序图例条目(例如可以通过 matplotlib.axes.Axes.get_legend_handles_labels 进行)?我想到的另一个可能的选择是在 matplotlib.pyplot.plot 中使用类似 zorder 选项的东西,但我不知道在 pygmt 中有类似的东西。

我希望这足以作为示例代码:

region,frame="g","g"
projection = 'G20.5/89.99999/25/4.5i' # yeah that’s the arctic…
fig = pygmt.Figure()
fig.coast(projection=projection, region=region, frame=frame, W=True) # plot cost lines
pen=0.9
fig.plot(cruise1_lon, cruise1_lat,label='"Cruise 1"',pen=('black',pen))
fig.plot(cruise2_lon, cruise2_lat,label='"Cruise 2"',pen=('green',pen))
fig.plot(cruise4_lon, cruise4_lat,label='"Cruise 4"',pen=('purple',pen)) # most recent one
fig.plot(cruise3_lon, cruise3_lat,label='"Cruise 3"',pen=('yellow',pen)) # should be plotted above the other cruises
fig.legend(position='JTL+jTL+o0.2c',box='+gwhite+p1p') 
# should be sorted as: Cruise 1, Cruise 2, Cruise 3, Cruise 4, 
# but is sorted as: Cruise 1, Cruise 2, Cruise 4, Cruise 3
fig.show()
4

1 回答 1

1

据我所知,到目前为止,通过 zorder 之类的选项是不可能的,但是,我将在 GitHub 上打开一个新功能请求。

不过,您的问题有一个解决方案。只需不为 Cruise4 设置标签,然后最后添加一个虚拟条目(具有相同值的 x,y 段),在其中将标签设置为 Cruise4。对于下面的示例,我生成了一些虚拟数据点。

import pygmt

region,frame="g","g"
projection = 'G20.5/89.99999/25/4.5i' # yeah that’s the arctic…
fig = pygmt.Figure()
fig.coast(projection=projection, region=region, frame=frame,shorelines=True) # plot cost lines
pen=3.9
fig.plot([90, 80, 25, 40], [90, 70, 85, 60], label='"Cruise1"',pen ('black',pen))
fig.plot([90, 20, 10, 50], [90, 80, 80, 60], label='"Cruise2"',pen=('green',pen))
fig.plot([90, 30, 30, 10], [90, 80, 70, 60], pen=('purple',pen)) # most recent one
fig.plot([90, 80, 30, 10], [80, 80, 80, 60], label='"Cruise3"',pen=('yellow',pen)) # should be plotted above the other cruises
# add dummy entry
fig.plot([90, 90], [90, 90], label='"Cruise 4"',pen=('purple',pen))
fig.legend(position='JTL+jTL+o0.2c',box='+gwhite+p1p')
# should be sorted as: Cruise 1, Cruise 2, Cruise 3, Cruise 4,
# but is sorted as: Cruise 1, Cruise 2, Cruise 4, Cruise 3
fig.show()

在此处输入图像描述

于 2021-09-15T17:25:36.320 回答