@@ -43,6 +43,19 @@ def update_from_first_child(tgt, src):
4343 tgt .update_from (first_child )
4444
4545
46+ def _first_color (colors ):
47+ if colors .size == 0 :
48+ return (0 , 0 , 0 , 0 )
49+ return tuple (colors [0 ])
50+
51+
52+ def _get_first (prop_array ):
53+ if len (prop_array ):
54+ return prop_array [0 ]
55+ else :
56+ return None
57+
58+
4659class HandlerBase :
4760 """
4861 A base class for default legend handlers.
@@ -427,6 +440,32 @@ def create_artists(self, legend, orig_handle,
427440 return [legline ]
428441
429442
443+ class HandlerPatchCollection (HandlerPatch ):
444+ """
445+ Handler for `.PatchCollection` instances.
446+ """
447+ def _default_update_prop (self , legend_handle , orig_handle ):
448+ lw = _get_first (orig_handle .get_linewidths ())
449+ dashes = _get_first (orig_handle ._us_linestyles )
450+ facecolor = _first_color (orig_handle .get_facecolor ())
451+ edgecolor = _first_color (orig_handle .get_edgecolor ())
452+ legend_handle .set_facecolor (facecolor )
453+ legend_handle .set_edgecolor (edgecolor )
454+ legend_handle .set_linestyle (dashes )
455+ legend_handle .set_linewidth (lw )
456+
457+ def create_artists (self , legend , orig_handle ,
458+ xdescent , ydescent , width , height , fontsize , trans ):
459+
460+ p = self ._create_patch (legend , orig_handle ,
461+ xdescent , ydescent , width , height , fontsize )
462+
463+ self .update_prop (p , orig_handle , legend )
464+ p .set_transform (trans )
465+
466+ return [p ]
467+
468+
430469class HandlerRegularPolyCollection (HandlerNpointsYoffsets ):
431470 r"""Handler for `.RegularPolyCollection`\s."""
432471
@@ -775,31 +814,20 @@ class HandlerPolyCollection(HandlerBase):
775814 `~.Axes.stackplot`.
776815 """
777816 def _update_prop (self , legend_handle , orig_handle ):
778- def first_color (colors ):
779- if colors .size == 0 :
780- return (0 , 0 , 0 , 0 )
781- return tuple (colors [0 ])
782-
783- def get_first (prop_array ):
784- if len (prop_array ):
785- return prop_array [0 ]
786- else :
787- return None
788-
789817 # orig_handle is a PolyCollection and legend_handle is a Patch.
790818 # Directly set Patch color attributes (must be RGBA tuples).
791- legend_handle ._facecolor = first_color (orig_handle .get_facecolor ())
792- legend_handle ._edgecolor = first_color (orig_handle .get_edgecolor ())
819+ legend_handle ._facecolor = _first_color (orig_handle .get_facecolor ())
820+ legend_handle ._edgecolor = _first_color (orig_handle .get_edgecolor ())
793821 legend_handle ._original_facecolor = orig_handle ._original_facecolor
794822 legend_handle ._original_edgecolor = orig_handle ._original_edgecolor
795823 legend_handle ._fill = orig_handle .get_fill ()
796824 legend_handle ._hatch = orig_handle .get_hatch ()
797825 # Hatch color is anomalous in having no getters and setters.
798826 legend_handle ._hatch_color = orig_handle ._hatch_color
799827 # Setters are fine for the remaining attributes.
800- legend_handle .set_linewidth (get_first (orig_handle .get_linewidths ()))
801- legend_handle .set_linestyle (get_first (orig_handle .get_linestyles ()))
802- legend_handle .set_transform (get_first (orig_handle .get_transforms ()))
828+ legend_handle .set_linewidth (_get_first (orig_handle .get_linewidths ()))
829+ legend_handle .set_linestyle (_get_first (orig_handle .get_linestyles ()))
830+ legend_handle .set_transform (_get_first (orig_handle .get_transforms ()))
803831 legend_handle .set_figure (orig_handle .get_figure ())
804832 # Alpha is already taken into account by the color attributes.
805833
0 commit comments