Skip to content

Add legend handler and artist for FancyArrow #10688

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/users/next_whats_new/more_legend_handlers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Add legend handlers for FancyArrowPatch, Text, and Annotation
-------------------------------------------------------------

By setting the label on FancyArrowPatch, Text, and Annotation objects
they will automatically be included in legends.
16 changes: 11 additions & 5 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import matplotlib.colors as colors
from matplotlib.font_manager import FontProperties
from matplotlib.lines import Line2D
from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch,
FancyArrowPatch)
from matplotlib.collections import (LineCollection, RegularPolyCollection,
CircleCollection, PathCollection,
PolyCollection)
Expand All @@ -50,6 +51,7 @@
from matplotlib.offsetbox import DraggableOffsetBox

from matplotlib.container import ErrorbarContainer, BarContainer, StemContainer
from matplotlib.text import Annotation, Text
from . import legend_handler


Expand Down Expand Up @@ -565,7 +567,6 @@ def _set_loc(self, loc):
# value of the find_offset.
self._loc_real = loc
self.stale = True
self._legend_box.set_offset(self._findoffset)

def _get_loc(self):
return self._loc_real
Expand Down Expand Up @@ -649,7 +650,10 @@ def _approx_text_height(self, renderer=None):
update_func=legend_handler.update_from_first_child),
tuple: legend_handler.HandlerTuple(),
PathCollection: legend_handler.HandlerPathCollection(),
PolyCollection: legend_handler.HandlerPolyCollection()
PolyCollection: legend_handler.HandlerPolyCollection(),
FancyArrowPatch: legend_handler.HandlerFancyArrowPatch(),
Text: legend_handler.HandlerText(),
Annotation: legend_handler.HandlerAnnotation()
}

# (get|set|update)_default_handler_maps are public interfaces to
Expand Down Expand Up @@ -837,6 +841,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
children=[self._legend_title_box,
self._legend_handle_box])
self._legend_box.set_figure(self.figure)
self._legend_box.set_offset(self._findoffset)
self.texts = text_list
self.legendHandles = handle_list

Expand Down Expand Up @@ -1142,12 +1147,13 @@ def _get_legend_handles(axs, legend_handler_map=None):
handles_original = []
for ax in axs:
handles_original += (ax.lines + ax.patches +
ax.collections + ax.containers)
ax.collections + ax.containers + ax.texts)
# support parasite axes:
if hasattr(ax, 'parasites'):
for axx in ax.parasites:
handles_original += (axx.lines + axx.patches +
axx.collections + axx.containers)
axx.collections + axx.containers +
axx.texts)

handler_map = Legend.get_default_handler_map()

Expand Down
Loading