From 3f3c34e745aab5ec7f9002ebdd8b7aab9936536d Mon Sep 17 00:00:00 2001 From: anika3943 Date: Sun, 20 Apr 2025 22:50:50 -0400 Subject: [PATCH] Update legend_handler --- lib/matplotlib/legend_handler.py | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 263945b050d0..0173c23960cc 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -810,3 +810,42 @@ def create_artists(self, legend, orig_handle, self.update_prop(p, orig_handle, legend) p.set_transform(trans) return [p] + +from matplotlib.legend_handler import HandlerPatchCollection + +_default_handler_map[PatchCollection] = HandlerPatchCollection() + + +from matplotlib.patches import Patch +from matplotlib.collections import PatchCollection + +class HandlerPatchCollection(HandlerPatch): + """ + Handler for PatchCollection, e.g. from matplotlib.collections. + Uses the properties of the first patch in the collection. + """ + + def create_artists(self, legend, orig_handle, + xdescent, ydescent, width, height, fontsize, trans): + p = Patch() + + try: + fc = orig_handle.get_facecolor()[0] + ec = orig_handle.get_edgecolor()[0] + lw = orig_handle.get_linewidths()[0] + ls = orig_handle.get_linestyle()[0] + except IndexError: + fc, ec, lw, ls = 'gray', 'black', 1.0, 'solid' + + p.set_facecolor(fc) + p.set_edgecolor(ec) + p.set_linewidth(lw) + p.set_linestyle(ls) + + self.update_prop(p, orig_handle, legend) + p.set_transform(trans) + p.set_xy([[-xdescent, -ydescent], + [-xdescent + width, -ydescent], + [-xdescent + width, -ydescent + height], + [-xdescent, -ydescent + height]]) + return [p] \ No newline at end of file