From c95bd90b47256767a2c683cb6ebb77b3c09bb561 Mon Sep 17 00:00:00 2001 From: emmamastro <57884530+emmamastro@users.noreply.github.com> Date: Wed, 28 Sep 2022 14:46:36 -0400 Subject: [PATCH 1/2] PathCollection addition and changes pathCollection changes --- lib/matplotlib/legend.py | 2 ++ lib/matplotlib/legend_handler.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 20d4a76e0db3..ff396d348d4d 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -665,6 +665,8 @@ def draw(self, renderer): ErrorbarContainer: legend_handler.HandlerErrorbar(), Line2D: legend_handler.HandlerLine2D(), Patch: legend_handler.HandlerPatch(), + #I added this (emma mastro) + PathCollection: legend_handler.HandlerPatchCollection(), StepPatch: legend_handler.HandlerStepPatch(), LineCollection: legend_handler.HandlerLineCollection(), RegularPolyCollection: legend_handler.HandlerRegularPolyCollection(), diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index c21c6d1212d3..6c287d5d2b93 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -328,6 +328,38 @@ def patch_func(legend=legend, orig_handle=orig_handle, super().__init__(**kwargs) self._patch_func = patch_func + + def _create_patch(self, legend, orig_handle, + xdescent, ydescent, width, height, fontsize): + if self._patch_func is None: + p = Rectangle(xy=(-xdescent, -ydescent), + width=width, height=height) + else: + p = self._patch_func(legend=legend, orig_handle=orig_handle, + xdescent=xdescent, ydescent=ydescent, + width=width, height=height, fontsize=fontsize) + return p + + def create_artists(self, legend, orig_handle, + xdescent, ydescent, width, height, fontsize, trans): + p = self._create_patch(legend, orig_handle, + xdescent, ydescent, width, height, fontsize) + self.update_prop(p, orig_handle, legend) + p.set_transform(trans) + return [p] +#I added this (emmamastro) +class HandlerPatchCollection(HandlerBase): + """ + Handler for Collection of `.Patch` instances. + """ + def _default_update_prop(self, legend_handle, orig_handle): + lw = orig_handle.get_linewidths()[0] + dashes = orig_handle._us_linestyles[0] + color = orig_handle.get_colors()[0] + legend_handle.set_color(color) + legend_handle.set_linestyle(dashes) + legend_handle.set_linewidth(lw) + def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize): if self._patch_func is None: From b9f22c49976955f6e15ffa10f7f6d0120b30cec4 Mon Sep 17 00:00:00 2001 From: emmamastro <57884530+emmamastro@users.noreply.github.com> Date: Wed, 28 Sep 2022 21:17:05 -0400 Subject: [PATCH 2/2] Patch Collection edits Patch collection edits --- lib/matplotlib/legend.py | 1 - lib/matplotlib/legend_handler.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index ff396d348d4d..0356efbaf045 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -665,7 +665,6 @@ def draw(self, renderer): ErrorbarContainer: legend_handler.HandlerErrorbar(), Line2D: legend_handler.HandlerLine2D(), Patch: legend_handler.HandlerPatch(), - #I added this (emma mastro) PathCollection: legend_handler.HandlerPatchCollection(), StepPatch: legend_handler.HandlerStepPatch(), LineCollection: legend_handler.HandlerLineCollection(), diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 6c287d5d2b93..8d6d25ca2be9 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -347,7 +347,7 @@ def create_artists(self, legend, orig_handle, self.update_prop(p, orig_handle, legend) p.set_transform(trans) return [p] -#I added this (emmamastro) + class HandlerPatchCollection(HandlerBase): """ Handler for Collection of `.Patch` instances.