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

Skip to content

Commit baf812c

Browse files
authored
Merge pull request #6639 from Kojoley/simplify-get_legend_handler
MNT: refactor get_legend_handler method
2 parents 7f4e84c + 55adc49 commit baf812c

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

lib/matplotlib/legend.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
from matplotlib import rcParams
3535
from matplotlib.artist import Artist, allow_rasterization
36-
from matplotlib.cbook import (is_string_like, iterable, silent_list, safezip)
36+
from matplotlib.cbook import (is_string_like, iterable, silent_list, safezip,
37+
is_hashable)
3738
from matplotlib.font_manager import FontProperties
3839
from matplotlib.lines import Line2D
3940
from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
@@ -565,19 +566,19 @@ def get_legend_handler(legend_handler_map, orig_handle):
565566
method-resolution-order. If no matching key is found, it
566567
returns None.
567568
"""
568-
legend_handler_keys = list(six.iterkeys(legend_handler_map))
569-
if orig_handle in legend_handler_keys:
570-
handler = legend_handler_map[orig_handle]
571-
else:
569+
if is_hashable(orig_handle):
570+
try:
571+
return legend_handler_map[orig_handle]
572+
except KeyError:
573+
pass
572574

573-
for handle_type in type(orig_handle).mro():
574-
if handle_type in legend_handler_map:
575-
handler = legend_handler_map[handle_type]
576-
break
577-
else:
578-
handler = None
575+
for handle_type in type(orig_handle).mro():
576+
try:
577+
return legend_handler_map[handle_type]
578+
except KeyError:
579+
pass
579580

580-
return handler
581+
return None
581582

582583
def _init_legend_box(self, handles, labels, markerfirst=True):
583584
"""

0 commit comments

Comments
 (0)