Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a734ed2

Browse files
authored
Merge pull request #20470 from kdpenner/legend-handlers
ENH: Issues warnings for legend handles without handlers
2 parents 8f1758b + 01a85b9 commit a734ed2

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/matplotlib/legend.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from matplotlib.collections import (
3939
Collection, CircleCollection, LineCollection, PathCollection,
4040
PolyCollection, RegularPolyCollection)
41+
from matplotlib.text import Text
4142
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
4243
from matplotlib.transforms import BboxTransformTo, BboxTransformFrom
4344
from matplotlib.offsetbox import (
@@ -740,11 +741,12 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
740741
handler = self.get_legend_handler(legend_handler_map, orig_handle)
741742
if handler is None:
742743
_api.warn_external(
743-
"Legend does not support {!r} instances.\nA proxy artist "
744-
"may be used instead.\nSee: "
745-
"https://matplotlib.org/users/legend_guide.html"
746-
"#creating-artists-specifically-for-adding-to-the-legend-"
747-
"aka-proxy-artists".format(orig_handle))
744+
"Legend does not support handles for {0} "
745+
"instances.\nA proxy artist may be used "
746+
"instead.\nSee: https://matplotlib.org/"
747+
"stable/tutorials/intermediate/legend_guide.html"
748+
"#controlling-the-legend-entries".format(
749+
type(orig_handle).__name__))
748750
# No handle for this artist, so we just defer to None.
749751
handle_list.append(None)
750752
else:
@@ -1074,14 +1076,14 @@ def _get_legend_handles(axs, legend_handler_map=None):
10741076
for ax in axs:
10751077
handles_original += [
10761078
*(a for a in ax._children
1077-
if isinstance(a, (Line2D, Patch, Collection))),
1079+
if isinstance(a, (Line2D, Patch, Collection, Text))),
10781080
*ax.containers]
10791081
# support parasite axes:
10801082
if hasattr(ax, 'parasites'):
10811083
for axx in ax.parasites:
10821084
handles_original += [
10831085
*(a for a in axx._children
1084-
if isinstance(a, (Line2D, Patch, Collection))),
1086+
if isinstance(a, (Line2D, Patch, Collection, Text))),
10851087
*axx.containers]
10861088

10871089
handler_map = {**Legend.get_default_handler_map(),
@@ -1091,6 +1093,15 @@ def _get_legend_handles(axs, legend_handler_map=None):
10911093
label = handle.get_label()
10921094
if label != '_nolegend_' and has_handler(handler_map, handle):
10931095
yield handle
1096+
elif (label not in ['_nolegend_', ''] and
1097+
not has_handler(handler_map, handle)):
1098+
_api.warn_external(
1099+
"Legend does not support handles for {0} "
1100+
"instances.\nSee: https://matplotlib.org/stable/"
1101+
"tutorials/intermediate/legend_guide.html"
1102+
"#implementing-a-custom-legend-handler".format(
1103+
type(handle).__name__))
1104+
continue
10941105

10951106

10961107
def _get_legend_handles_labels(axs, legend_handler_map=None):

lib/matplotlib/tests/test_legend.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,15 @@ def test_handler_numpoints():
506506
ax.legend(numpoints=0.5)
507507

508508

509+
def test_text_nohandler_warning():
510+
"""Test that Text artists with labels raise a warning"""
511+
fig, ax = plt.subplots()
512+
ax.text(x=0, y=0, s="text", label="label")
513+
with pytest.warns(UserWarning) as record:
514+
ax.legend()
515+
assert len(record) == 1
516+
517+
509518
def test_empty_bar_chart_with_legend():
510519
"""Test legend when bar chart is empty with a label."""
511520
# related to issue #13003. Calling plt.legend() should not

lib/matplotlib/text.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ def __init__(self,
148148
"""
149149
Create a `.Text` instance at *x*, *y* with string *text*.
150150
151+
While Text accepts the 'label' keyword argument, by default it is not
152+
added to the handles of a legend.
153+
151154
Valid keyword arguments are:
152155
153156
%(Text:kwdoc)s

0 commit comments

Comments
 (0)