Skip to content

Legend does not show 'annotate' #8236

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

Closed
jlecoeur opened this issue Mar 8, 2017 · 5 comments · May be fixed by #23160
Closed

Legend does not show 'annotate' #8236

jlecoeur opened this issue Mar 8, 2017 · 5 comments · May be fixed by #23160
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! New feature status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action

Comments

@jlecoeur
Copy link

jlecoeur commented Mar 8, 2017

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)

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Mar 8, 2017
@tacaswell
Copy link
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?

@tacaswell tacaswell added New feature Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues new-contributor-friendly labels Mar 8, 2017
@tacaswell
Copy link
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).

@bendichter
Copy link

bendichter commented Mar 11, 2017

@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

@jlecoeur
Copy link
Author

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

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Sep 24, 2017
@tacaswell tacaswell added the Good first issue Open a pull request against these issues if there are no active ones! label Oct 16, 2017
@SwarnaD SwarnaD mentioned this issue Mar 15, 2018
6 tasks
Wojciech-Lange added a commit to Wojciech-Lange/matplotlib that referenced this issue Dec 21, 2018
@github-actions
Copy link

github-actions bot commented Apr 8, 2023

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Apr 8, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2023
@rcomer rcomer added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! New feature status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action
Projects
None yet
5 participants