@@ -651,38 +651,24 @@ def draw(self, renderer):
651651
652652 @classmethod
653653 def get_default_handler_map (cls ):
654- """
655- A class method that returns the default handler map.
656- """
654+ """Return the global default handler map, shared by all legends."""
657655 return cls ._default_handler_map
658656
659657 @classmethod
660658 def set_default_handler_map (cls , handler_map ):
661- """
662- A class method to set the default handler map.
663- """
659+ """Set the global default handler map, shared by all legends."""
664660 cls ._default_handler_map = handler_map
665661
666662 @classmethod
667663 def update_default_handler_map (cls , handler_map ):
668- """
669- A class method to update the default handler map.
670- """
664+ """Update the global default handler map, shared by all legends."""
671665 cls ._default_handler_map .update (handler_map )
672666
673667 def get_legend_handler_map (self ):
674- """
675- Return the handler map.
676- """
677-
668+ """Return this legend instance's handler map."""
678669 default_handler_map = self .get_default_handler_map ()
679-
680- if self ._custom_handler_map :
681- hm = default_handler_map .copy ()
682- hm .update (self ._custom_handler_map )
683- return hm
684- else :
685- return default_handler_map
670+ return ({** default_handler_map , ** self ._custom_handler_map }
671+ if self ._custom_handler_map else default_handler_map )
686672
687673 @staticmethod
688674 def get_legend_handler (legend_handler_map , orig_handle ):
@@ -1105,11 +1091,7 @@ def get_draggable(self):
11051091# Helper functions to parse legend arguments for both `figure.legend` and
11061092# `axes.legend`:
11071093def _get_legend_handles (axs , legend_handler_map = None ):
1108- """
1109- Return a generator of artists that can be used as handles in
1110- a legend.
1111-
1112- """
1094+ """Yield artists that can be used as handles in a legend."""
11131095 handles_original = []
11141096 for ax in axs :
11151097 handles_original += [
@@ -1124,28 +1106,19 @@ def _get_legend_handles(axs, legend_handler_map=None):
11241106 if isinstance (a , (Line2D , Patch , Collection ))),
11251107 * axx .containers ]
11261108
1127- handler_map = Legend .get_default_handler_map ()
1128-
1129- if legend_handler_map is not None :
1130- handler_map = handler_map .copy ()
1131- handler_map .update (legend_handler_map )
1132-
1109+ handler_map = {** Legend .get_default_handler_map (),
1110+ ** (legend_handler_map or {})}
11331111 has_handler = Legend .get_legend_handler
1134-
11351112 for handle in handles_original :
11361113 label = handle .get_label ()
11371114 if label != '_nolegend_' and has_handler (handler_map , handle ):
11381115 yield handle
11391116
11401117
11411118def _get_legend_handles_labels (axs , legend_handler_map = None ):
1142- """
1143- Return handles and labels for legend, internal method.
1144-
1145- """
1119+ """Return handles and labels for legend."""
11461120 handles = []
11471121 labels = []
1148-
11491122 for handle in _get_legend_handles (axs , legend_handler_map ):
11501123 label = handle .get_label ()
11511124 if label and not label .startswith ('_' ):
@@ -1201,7 +1174,7 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
12011174 """
12021175 log = logging .getLogger (__name__ )
12031176
1204- handlers = kwargs .get ('handler_map' , {}) or {}
1177+ handlers = kwargs .get ('handler_map' )
12051178 extra_args = ()
12061179
12071180 if (handles is not None or labels is not None ) and args :
0 commit comments