0

I want to create a map with pygmt, where I show the tracks of 4 cruises. They do partially overlap and one cruise is of special interest to me, hence I like to have it on top. The legend, however, should be sorted by date but the desired cruise was not the most recent one. Is there a way in pygmt to reorder the legend entries (as one can do for example by means of matplotlib.axes.Axes.get_legend_handles_labels)? Another possible option that came to my mind was the usage of something like the zorder option in matplotlib.pyplot.plot but I'm not aware of something similar in pygmt.

I hope this suffices as example code:

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()
hänz
  • 1

1 Answers1

1

As far as I know that's not possible so far via an option like zorder, however, I will open a new feature request on GitHub.

Nevertheless there's a solution for your problem. Simply set no label for Cruise4 and then add finally a dummy entry (x,y segment with same values) in which you set the label to Cruise4. For the example below I generated some dummy data points.

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()

enter image description here

mgrund
  • 1,415
  • 8
  • 10