diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 59fbb21a4de4..9e5b903d59ab 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1140,7 +1140,7 @@ def _get_legend_handles(axs, legend_handler_map=None): label = handle.get_label() if label != '_nolegend_' and has_handler(handler_map, handle): yield handle - elif (label not in ['_nolegend_', ''] and + elif (label and not label.startswith('_') and not has_handler(handler_map, handle)): _api.warn_external( "Legend does not support handles for {0} " diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index dff45126bf56..16847e0be6e2 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -1,6 +1,7 @@ import collections import platform from unittest import mock +import warnings import numpy as np import pytest @@ -515,6 +516,13 @@ def test_text_nohandler_warning(): ax.legend() assert len(record) == 1 + # this should _not_ warn: + f, ax = plt.subplots() + ax.pcolormesh(np.random.uniform(0, 1, (10, 10))) + with warnings.catch_warnings(): + warnings.simplefilter("error") + ax.get_legend_handles_labels() + def test_empty_bar_chart_with_legend(): """Test legend when bar chart is empty with a label."""