8000 Issues warning if Artist is Text or is not a Rectangle · matplotlib/matplotlib@46a102d · GitHub
[go: up one dir, main page]

Skip to content

Commit 46a102d

Browse files
committed
Issues warning if Artist is Text or is not a Rectangle
1 parent 87b9118 commit 46a102d

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

lib/matplotlib/legend.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from matplotlib.lines import Line2D
3636
from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch,
3737
StepPatch)
38+
from matplotlib.text import Text
3839
from matplotlib.collections import (
3940
Collection, CircleCollection, LineCollection, PathCollection,
4041
PolyCollection, RegularPolyCollection)
@@ -642,7 +643,7 @@ def draw(self, renderer):
642643
StemContainer: legend_handler.HandlerStem(),
643644
ErrorbarContainer: legend_handler.HandlerErrorbar(),
644645
Line2D: legend_handler.HandlerLine2D(),
645-
Patch: legend_handler.HandlerPatch(),
646+
Rectangle: legend_handler.HandlerPatch(),
646647
StepPatch: legend_handler.HandlerStepPatch(),
647648
LineCollection: legend_handler.HandlerLineCollection(),
648649
RegularPolyCollection: legend_handler.HandlerRegularPolyCollection(),
@@ -763,28 +764,35 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
763764
for orig_handle, lab in zip(handles, labels):
764765
handler = self.get_legend_handler(legend_handler_map, orig_handle)
765766
if handler is None:
766-
_api.warn_external(
767-
"Legend does not support {!r} instances.\nA proxy artist "
768-
"may be used instead.\nSee: "
769-
"https://matplotlib.org/users/legend_guide.html"
770-
"#creating-artists-specifically-for-adding-to-the-legend-"
771-
"aka-proxy-artists".format(orig_handle))
772-
# We don't have a handle for this artist, so we just defer
773-
# to None.
774-
handle_list.append(None)
775-
else:
776-
textbox = TextArea(lab, textprops=label_prop,
777-
multilinebaseline=True)
778-
handlebox = DrawingArea(width=self.handlelength * fontsize,
779-
height=height,
780-
xdescent=0., ydescent=descent)
781-
782-
text_list.append(textbox._text)
783-
# Create the artist for the legend which represents the
784-
# original artist/handle.
785-
handle_list.append(handler.legend_artist(self, orig_handle,
786-
fontsize, handlebox))
787-
handles_and_labels.append((handlebox, textbox))
767+
if isinstance(orig_handle, Text):
768+
_api.warn_external(
769+
"Legend does not support handles for Text "
770+
"instances.\nSee: https://matplotlib.org/stable/"
771+
"tutorials/intermediate/legend_guide.html"
772+
"#implementing-a-custom-legend-handler")
773+
continue
774+
else:
775+
_api.warn_external(
776+
"Legend does not support handles for {0} "
777+
"instances.\nA Rectangle proxy artist will be "
778+
"used instead.\nSee: https://matplotlib.org/"
779+
"stable/tutorials/intermediate/legend_guide.html"
780+
"#controlling-the-legend-entries".format(
781+
type(orig_handle).__name__))
782+
handler = self.get_legend_handler(legend_handler_map,
783+
Rectangle)
784+
textbox = TextArea(lab, textprops=label_prop,
785+
10000 multilinebaseline=True)
786+
handlebox = DrawingArea(width=self.handlelength * fontsize,
787+
height=height,
788+
xdescent=0., ydescent=descent)
789+
790+
text_list.append(textbox._text)
791+
# Create the artist for the legend which represents the
792+
# original artist/handle.
793+
handle_list.append(handler.legend_artist(self, orig_handle,
794+
fontsize, handlebox))
795+
handles_and_labels.append((handlebox, textbox))
788796

789797
if handles_and_labels:
790798
# We calculate number of rows in each column. The first
@@ -1141,27 +1149,19 @@ def _get_legend_handles(axs, legend_handler_map=None):
11411149
for ax in axs:
11421150
handles_original += [
11431151
*(a for a in ax._children
1144-
if isinstance(a, (Line2D, Patch, Collection))),
1152+
if isinstance(a, (Line2D, Patch, Collection, Text))),
11451153
*ax.containers]
11461154
# support parasite axes:
11471155
if hasattr(ax, 'parasites'):
11481156
for axx in ax.parasites:
11491157
handles_original += [
11501158
*(a for a in axx._children
1151-
if isinstance(a, (Line2D, Patch, Collection))),
1159+
if isinstance(a, (Line2D, Patch, Collection, Text))),
11521160
*axx.containers]
11531161

1154-
handler_map = Legend.get_default_handler_map()
1155-
1156-
if legend_handler_map is not None:
1157-
handler_map = handler_map.copy()
1158-
handler_map.update(legend_handler_map)
1159-
1160-
has_handler = Legend.get_legend_handler
1161-
11621162
for handle in handles_original:
11631163
label = handle.get_label()
1164-
if label != '_nolegend_' and has_handler(handler_map, handle):
1164+
if label not in ['_nolegend_', '']:
11651165
yield handle
11661166

11671167

lib/matplotlib/text.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def __init__(self,
130130
"""
131131
Create a `.Text` instance at *x*, *y* with string *text*.
132132
133+
While Text accepts the `label` keyword argument, it is not added to the
134+
handles of a legend by default.
135+
133136
Valid keyword arguments are:
134137
135138
%(Text:kwdoc)s

0 commit comments

Comments
 (0)
0