8000 Issue warning for Text with a label · matplotlib/matplotlib@e82af34 · GitHub
[go: up one dir, main page]

Skip to content

Commit e82af34

Browse files
committed
Issue warning for Text with a label
1 parent 8c2a65d commit e82af34

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/matplotlib/legend.py

Lines changed: 17 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 (
@@ -748,11 +749,12 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
748749
handler = self.get_legend_handler(legend_handler_map, orig_handle)
749750
if handler is None:
750751
_api.warn_external(
751-
"Legend does not support {!r} instances.\nA proxy artist "
752-
"may be used instead.\nSee: "
753-
"https://matplotlib.org/users/legend_guide.html"
754-
"#creating-artists-specifically-for-adding-to-the-legend-"
755-
"aka-proxy-artists".format(orig_handle))
752+
"Legend does not support handles for {0} "
753+
"instances.\nA proxy artist may be used "
754+
"instead.\nSee: https://matplotlib.org/"
755+
"stable/tutorials/intermediate/legend_guide.html"
756+
"#controlling-the-legend-entries".format(
757+
type(orig_handle).__name__))
756758
# We don't have a handle for this artist, so we just defer
757759
# to None.
758760
handle_list.append(None)
@@ -1102,14 +1104,14 @@ def _get_legend_handles(axs, legend_handler_map=None):
11021104
for ax in axs:
11031105
handles_original += [
11041106
*(a for a in ax._children
1105-
if isinstance(a, (Line2D, Patch, Collection))),
1107+
if isinstance(a, (Line2D, Patch, Collection, Text))),
11061108
*ax.containers]
11071109
# support parasite axes:
11081110
if hasattr(ax, 'parasites'):
11091111
for axx in ax.parasites:
11101112
handles_original += [
11111113
*(a for a in axx._children
1112-
if isinstance(a, (Line2D, Patch, Collection))),
1114+
if isinstance(a, (Line2D, Patch, Collection, Text))),
11131115
*axx.containers]
11141116

11151117
handler_map = {**Legend.get_default_handler_map(),
@@ -1119,6 +1121,14 @@ def _get_legend_handles(axs, legend_handler_map=None):
11191121
label = handle.get_label()
11201122
if label != '_nolegend_' and has_handler(handler_map, handle):
11211123
yield handle
1124+
elif not has_handler(handler_map, handle):
1125+
_api.warn_external(
1126+
"Legend does not support handles for {0} "
1127+
"instances.\nSee: https://matplotlib.org/stable/"
1128+
"tutorials/intermediate/legend_guide.html"
1129+
"#implementing-a-custom-legend-handler".format(
1130+
type(handle).__name__))
1131+
continue
11221132

11231133

11241134
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)
0