From 5badcf930b776a7be050c5f0cf6bf3041b5a58dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A7=80=ED=97=8C?= Date: Wed, 8 Jul 2026 09:37:28 +0900 Subject: [PATCH] ENH/MAINT: Improve legend handling in Qt figure options dialog Closes #17775: Clone additional legend properties (loc, title, frameon, shadow, edgecolor, facecolor, framealpha) when regenerating the legend, so that user-configured settings are not lost on Apply. Closes #11109: Add a 'Show legend' checkbox to the Axes tab, allowing users to toggle legend visibility without deleting it. --- .../backends/qt_editor/figureoptions.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/qt_editor/figureoptions.py b/lib/matplotlib/backends/qt_editor/figureoptions.py index cd4e9583cce5..1871829197db 100644 --- a/lib/matplotlib/backends/qt_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt_editor/figureoptions.py @@ -61,6 +61,7 @@ def convert_limits(lim, converter): ) for name, axis in axis_map.items() ]), + ('Show legend', axes.legend_ is not None and axes.legend_.get_visible()), ('(Re-)Generate automatic legend', False), ] @@ -196,6 +197,7 @@ def apply_callback(data): title = general.pop(0) axes.title.set_text(title) generate_legend = general.pop() + show_legend = general.pop() for i, (name, axis) in enumerate(axis_map.items()): axis_min = general[4*i] @@ -248,14 +250,31 @@ def apply_callback(data): if generate_legend: draggable = None ncols = 1 + kwargs = {} if axes.legend_ is not None: old_legend = axes.get_legend() draggable = old_legend._draggable is not None ncols = old_legend._ncols - new_legend = axes.legend(ncols=ncols) + kwargs = { + 'loc': old_legend._loc_real, + 'title': old_legend.get_title().get_text(), + 'frameon': old_legend.get_frame_on(), + 'shadow': old_legend.shadow, + } + # Clone frame properties if the frame exists + frame = old_legend.get_frame() + if frame is not None: + kwargs['edgecolor'] = frame.get_edgecolor() + kwargs['facecolor'] = frame.get_facecolor() + kwargs['framealpha'] = frame.get_alpha() + + new_legend = axes.legend(ncols=ncols, **kwargs) if new_legend: new_legend.set_draggable(draggable) + if axes.legend_ is not None: + axes.legend_.set_visible(show_legend) + # Redraw figure = axes.get_figure() figure.canvas.draw()