From 55adc4932905b86167905b89e032cf60e19c25c0 Mon Sep 17 00:00:00 2001 From: Nikita Kniazev Date: Fri, 24 Jun 2016 22:29:39 +0300 Subject: [PATCH] Simplify get_legend_handler method --- lib/matplotlib/legend.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 65ec29b136a..a6f3f023079 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -33,7 +33,8 @@ from matplotlib import rcParams from matplotlib.artist import Artist, allow_rasterization -from matplotlib.cbook import (is_string_like, iterable, silent_list, safezip) +from matplotlib.cbook import (is_string_like, iterable, silent_list, safezip, + is_hashable) from matplotlib.font_manager import FontProperties from matplotlib.lines import Line2D from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch @@ -565,19 +566,19 @@ def get_legend_handler(legend_handler_map, orig_handle): method-resolution-order. If no matching key is found, it returns None. """ - legend_handler_keys = list(six.iterkeys(legend_handler_map)) - if orig_handle in legend_handler_keys: - handler = legend_handler_map[orig_handle] - else: + if is_hashable(orig_handle): + try: + return legend_handler_map[orig_handle] + except KeyError: + pass - for handle_type in type(orig_handle).mro(): - if handle_type in legend_handler_map: - handler = legend_handler_map[handle_type] - break - else: - handler = None + for handle_type in type(orig_handle).mro(): + try: + return legend_handler_map[handle_type] + except KeyError: + pass - return handler + return None def _init_legend_box(self, handles, labels, markerfirst=True): """