Skip to content

Legend does not show 'annotate' #8236

Closed as not planned
Closed as not planned

Description

@jlecoeur

When using annotate to draw an arrow, it does not show up in the legend.

Example code:

import matplotlib.pylab as plt
%matplotlib inline

fig, ax = plt.subplots(1)
ax.plot([0, 1], [0, 0], label='line1')
ax.plot([0, 1], [1, 1], label='line2')
ax.annotate("",
            xy=(0.4,1.0),
            xytext=(0.4,0.0),
            arrowprops={'arrowstyle':'<->', 'color':'C7' },
            label='distance')
ax.legend()

gives:
annotate

I would expect an entry "distance" in the legend, with a grey double-sided arrow as symbol.

(I use Python 3.6.0 and Matplotlib 2.0.0 from Archlinux repository)

Activity

  1. added this to the milestone on Mar 8, 2017
  2. tacaswell commented on Mar 8, 2017

    @tacaswell
    Member

    Because they are not in the list of things the that are looked for to be added to the legend by default. Start from https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L546 and trace back.

    You will probably also have to add a LegendHandler.

    Exact work:

    • get annotations to be included in the list of artists found by Axes.get_legend_handles_labels
    • write a legend handler to display annotations in a nice way.

    @jlecoeur want to take a crack at this?

  3. tacaswell commented on Mar 8, 2017

    @tacaswell
    Member

    Tagged as new contributor friendly because it is self-contained, but medium difficulty as it involves understanding the legend machinery (which is powerful, but a bit under-documented).

  4. bendichter commented on Mar 11, 2017

    @bendichter

    @jlecoeur: You can achieve something close to what you are looking for with:

    import matplotlib.pylab as plt
    %matplotlib inline
    
    fig, ax = plt.subplots(1)
    h1, = ax.plot([0, 1], [0, 0], label='line1')
    h2, = ax.plot([0, 1], [1, 1], label='line2')
    h3 = ax.annotate("",
                xy=(0.4,1.0),
                xytext=(0.4,0.0),
                arrowprops={'arrowstyle':'<->', 'color':'C7' },
                label='distance')
    ax.legend([h1, h2, h3.arrow_patch], (h1.get_label(),h2.get_label(), 'c'))
    

    It would be better if legend found the annotation arrow automatically. It would also be better if legend handled FancyArrowPatches better. They are all drawn as rectangles, which is the default for patches, but they should draw little arrows in the legend.

    I'm new to this, so here are my thoughts on how to proceed:

    1. create HandlerFancyArrowPatch in legend_handler.py that handles fancy arrow patches in general, drawing the appropriate arrow

    2. create HandlerAnnotation that takes an Annotation object and calls HandlerFancyArrowPatch if the annotation has an arrow associated with it

  5. jlecoeur commented on Mar 12, 2017

    @jlecoeur
    Author

    Thanks @bendichter @tacaswell, I will try to implement it and I'll let you know!

  6. modified the milestones: , on Sep 24, 2017
  7. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Oct 16, 2017
  8. added a commit that references this issue on Dec 21, 2018
  9. github-actions commented on Apr 8, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  10. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesNew featurestatus: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions