Support for legend with "handles" and "labels" like MATLAB/matplotlib
See original GitHub issueDescription of the desired feature
A wrapper for the GMT 6 legend
command with support for specifying “handles” and “labels” as arguments, as in MATLAB (legend docs) and matplotlib (legend docs). The plotting commands (i.e., gmt.Figure.plot()
, for now) are modified to return “handles” which are provided as inputs to gmt.Figure.legend()
. This addresses part of #214 and #231.
Below is a minimal working example using my add-legend
fork:
import gmt
fig = gmt.Figure()
h1 = fig.plot(x=[-5], y=[5], region=[-10, 10, -5, 10], projection='X3i/0', frame='a',
style='a15p', pen='1.5p,purple')
h2 = fig.plot(x=[0], y=[5],
style='t10p', color='cyan')
h3 = fig.plot(x=[5], y=[5],
style='d5p', color='yellow', pen=True)
fig.legend([[h1, h2, h3], ['I am a star!', 'I am a triangle!', 'I am a diamond!']],
F=True, D='g0/0+w2i+jCM')
fig.show()
Are you willing to help implement and maintain this feature?
Yes. You can view the source code for my implementation here.
The good news:
- The legend wrapper accepts either the standard specfile (see GMT legend docs) or a list containing a list of handles and a list of labels. So folks can still build a complex legend by creating a specfile.
- The only modification to
gmt.Figure.plot()
is that it now returnskwargs
. So existing users won’t have to change their syntax for the plot command.
The bad news:
- This implementation makes use of
GMTTempFile
since I don’t know how to do it any other way (yet). - Right now I’m using regular expression matching (via
re
) to separate symbol type and size from thestyle
argument ofgmt.Figure.plot()
, which is probably really sketchy. - This only works for symbol entries in the legend, i.e. entries in specfile which have the form:
S - symbol size fill pen - text
I feel like this covers a broad portion of typical legend use cases, though. - I have not written any tests.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:18 (17 by maintainers)
Top Results From Across the Web
Customizing Plot Legends | Python Data Science Handbook
We previously saw how to create a simple legend; here we'll take a look at customizing the placement and aesthetics of the legend...
Read more >How to insert legend in matplotlib - Educative.io
Using legend(labels): These are used to label existing elements. This method can be used to create a legend for the elements already existing...
Read more >matplotlib.pyplot.legend — Matplotlib 3.6.2 documentation
A list of labels to show next to the artists. Use this together with handles, if you need full control on what is...
Read more >No handles with labels found to put in legend - Stack Overflow
@Yehuda Yes, plt.arrow() works like plt.plot(), and that is being asked for. It is strange why this solution is upvoted so much since...
Read more >Add legend to axes - MATLAB legend - MathWorks
Plot two lines. Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. Then, add a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
P.S. If you’d like to understand testing a bit more, I highly recommend Automation Panda’s Python Testing 101 series. In particular, the introduction and pytest one will be relevant for PyGMT. He’s also got some videos if you prefer that style of learning.
A legend function is something I’ve been looking for of late. I’m more than happy to help test any of the implementations mentioned above when they become available.