Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to reorder legend entries #1513

Open
michaelgrund opened this issue Sep 16, 2021 · 0 comments
Open

Allow to reorder legend entries #1513

michaelgrund opened this issue Sep 16, 2021 · 0 comments
Labels
feature request New feature wanted question Further information is requested

Comments

@michaelgrund
Copy link
Member

Description of the desired feature

In a recent Stack Overflow post the question came up if it's possible to reorder the legend entries in PyGMT (see example below, left plot). As far as I know in principle the legend entries follow the order in which the corrsponding fig.plot() calls (including the label definition) take place. However, I made a suggestion using the following workaround (right plot):

import pygmt

region,frame="g","g"
projection = 'G20.5/89.99999/25/4.5i' # yeah that’s the arctic…

# original figure
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='"Cruise 1"',pen=('black',pen))
fig.plot([90, 20, 10, 50], [90, 80, 80, 60], label='"Cruise 2"',pen=('green',pen))
fig.plot([90, 30, 30, 10], [90, 80, 70, 60], label='"Cruise 4"',pen=('purple',pen)) # most recent one
fig.plot([90, 80, 30, 10], [80, 80, 80, 60], 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.shift_origin(xshift="w+0.5c")

# workaround to fix the problem
fig.coast(projection=projection, region=region, frame=frame, shorelines=True)
fig.plot([90, 80, 25, 40], [90, 70, 85, 60], label='"Cruise 1"',pen=('black',pen))
fig.plot([90, 20, 10, 50], [90, 80, 80, 60], label='"Cruise 2"',pen=('green',pen))
fig.plot([90, 30, 30, 10], [90, 80, 70, 60], pen=('purple',pen)) 
fig.plot([90, 80, 30, 10], [80, 80, 80, 60], label='"Cruise 3"',pen=('yellow',pen)) 

# add dummy entry for legend entry
fig.plot([90, 90], [90, 90], label='"Cruise 4"',pen=('purple',pen)) 

fig.legend(position='JTL+jTL+o0.2c',box='+gwhite+p1p') 
fig.show()

PLOT_test

I guess it's not possible to directly implement something like the zorder option in matplotlib.pyplot.plot in PyGMT since the module is connected to GMT's plot function. However, maybe there are some ideas how we could handle that.

Are you willing to help implement and maintain this feature? First discuss

@michaelgrund michaelgrund added the feature request New feature wanted label Sep 16, 2021
@michaelgrund michaelgrund added the question Further information is requested label Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature wanted question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant