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

Skip to content

Commit 01a85b9

Browse files
committed
Issues warnings for legend handles without handlers
1 parent b525983 commit 01a85b9

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
@@ -493,6 +493,15 @@ def test_handler_numpoints():
493493
ax.legend(numpoints=0.5)
494494

495495

496+
def test_text_nohandler_warning():
497+
"""Test that Text artists with labels raise a warning"""
498+
fig, ax = plt.subplots()
499+
ax.text(x=0, y=0, s="text", label="label")
500+
with pytest.warns(UserWarning) as record:
501+
ax.legend()
502+
assert len(record) == 1
503+
504+
496505
def test_empty_bar_chart_with_legend():
497506
"""Test legend when bar chart is empty with a label."""
498507
# 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
@@ -132,6 +132,9 @@ def __init__(self,
132132
"""
133133
Create a `.Text` instance at *x*, *y* with string *text*.
134134
135+
While Text accepts the 'label' keyword argument, by default it is not
136+
added to the handles of a legend.
137+
135138
Valid keyword arguments are:
136139
137140
%(Text:kwdoc)s

0 commit comments

Comments
 (0)