Skip to content

Commit 07af522

Browse files
authored
Merge pull request #23762 from jklymak/fix-legend-warning
FIX: legend handler warning too liberal
2 parents ddd8fb8 + 7805ac8 commit 07af522

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/matplotlib/legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ def _get_legend_handles(axs, legend_handler_map=None):
11401140
label = handle.get_label()
11411141
if label != '_nolegend_' and has_handler(handler_map, handle):
11421142
yield handle
1143-
elif (label not in ['_nolegend_', ''] and
1143+
elif (label and not label.startswith('_') and
11441144
not has_handler(handler_map, handle)):
11451145
_api.warn_external(
11461146
"Legend does not support handles for {0} "

lib/matplotlib/tests/test_legend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import collections
22
import platform
33
from unittest import mock
4+
import warnings
45

56
import numpy as np
67
import pytest
@@ -515,6 +516,13 @@ def test_text_nohandler_warning():
515516
ax.legend()
516517
assert len(record) == 1
517518

519+
# this should _not_ warn:
520+
f, ax = plt.subplots()
521+
ax.pcolormesh(np.random.uniform(0, 1, (10, 10)))
522+
with warnings.catch_warnings():
523+
warnings.simplefilter("error")
524+
ax.get_legend_handles_labels()
525+
518526

519527
def test_empty_bar_chart_with_legend():
520528
"""Test legend when bar chart is empty with a label."""

0 commit comments

Comments
 (0)