From c55ca9fd64f74c4bb9ad57ea371ae8a1ec07b7d0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 18 Mar 2020 12:12:18 +0100 Subject: [PATCH] Dedupe implementations of configure_subplots(). Rely on pyplot auto-backend-detection to pop up a window with the correct canvas class (technically this means one can end up with a gtk3agg subplot tool when the main figure is gtk3cairo, but that shouldn't be a real problem...). - Qt is excluded because it has its own (native) subplot config tool. - wx needs to restore a call to Destroy() because the subplot config figure is not pyplot-managed, so `Gcf.destroy(self.num)` is a noop. - Add a reference to the subplot config figure from the subplot tool widget. --- lib/matplotlib/backend_bases.py | 5 +++++ lib/matplotlib/backends/_backend_tk.py | 10 --------- lib/matplotlib/backends/backend_gtk3.py | 28 ------------------------- lib/matplotlib/backends/backend_wx.py | 20 ------------------ lib/matplotlib/pyplot.py | 20 +++++++++--------- lib/matplotlib/widgets.py | 1 + 6 files changed, 16 insertions(+), 68 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 37d7d276ea93..7a434d5b22ad 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3245,6 +3245,11 @@ def _update_view(self): ax._set_position(pos_active, 'active') self.canvas.draw_idle() + def configure_subplots(self, *args): + from matplotlib import pyplot as plt + tool = plt.subplot_tool(self.canvas.figure) + tool.figure.canvas.manager.show() + def save_figure(self, *args): """Save the current figure.""" raise NotImplementedError diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index bc31d31e699b..3209d7db7ae4 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -583,16 +583,6 @@ def _Spacer(self): s.pack(side=tk.LEFT, padx=5) return s - def configure_subplots(self): - toolfig = Figure(figsize=(6, 3)) - window = tk.Toplevel() - canvas = type(self.canvas)(toolfig, master=window) - toolfig.subplots_adjust(top=0.9) - canvas.tool = SubplotTool(self.canvas.figure, toolfig) - canvas.draw() - canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) - window.grab_set() - def save_figure(self, *args): filetypes = self.canvas.get_supported_filetypes().copy() default_filetype = self.canvas.get_default_filetype() diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index 62eb324ebce1..a1a1c6544e25 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -587,34 +587,6 @@ def on_notify_filter(*args): except Exception as e: error_msg_gtk(str(e), parent=self) - def configure_subplots(self, button): - toolfig = Figure(figsize=(6, 3)) - canvas = type(self.canvas)(toolfig) - toolfig.subplots_adjust(top=0.9) - # Need to keep a reference to the tool. - _tool = SubplotTool(self.canvas.figure, toolfig) - - w = int(toolfig.bbox.width) - h = int(toolfig.bbox.height) - - window = Gtk.Window() - try: - window.set_icon_from_file(window_icon) - except Exception: - # we presumably already logged a message on the - # failure of the main plot, don't keep reporting - pass - window.set_title("Subplot Configuration Tool") - window.set_default_size(w, h) - vbox = Gtk.Box() - vbox.set_property("orientation", Gtk.Orientation.VERTICAL) - window.add(vbox) - vbox.show() - - canvas.show() - vbox.pack_start(canvas, True, True, 0) - window.show() - def set_history_buttons(self): can_backward = self._nav_stack._pos > 0 can_forward = self._nav_stack._pos < len(self._nav_stack._elements) - 1 diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index f1887a886576..099ae5e6fe11 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -1186,26 +1186,6 @@ def pan(self, *args): self.ToggleTool(self.wx_ids['Zoom'], False) NavigationToolbar2.pan(self, *args) - def configure_subplots(self, *args): - global FigureManager # placates pyflakes: created by @_Backend.export - frame = wx.Frame(None, -1, "Configure subplots") - _set_frame_icon(frame) - - toolfig = Figure((6, 3)) - canvas = type(self.canvas)(frame, -1, toolfig) - - # Create a figure manager to manage things - FigureManager(canvas, 1, frame) - - # Now put all into a sizer - sizer = wx.BoxSizer(wx.VERTICAL) - # This way of adding to sizer allows resizing - sizer.Add(canvas, 1, wx.LEFT | wx.TOP | wx.GROW) - frame.SetSizer(sizer) - frame.Fit() - SubplotTool(self.canvas.figure, toolfig) - frame.Show() - def save_figure(self, *args): # Fetch the required filename and file type. filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards() diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 1314f055ec5d..3e9088073cb9 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -1453,19 +1453,19 @@ def subplot_tool(targetfig=None): """ Launch a subplot tool window for a figure. - A :class:`matplotlib.widgets.SubplotTool` instance is returned. + A `matplotlib.widgets.SubplotTool` instance is returned. """ if targetfig is None: targetfig = gcf() - - with rc_context({'toolbar': 'None'}): # No nav toolbar for the toolfig. - toolfig = figure(figsize=(6, 3)) - toolfig.subplots_adjust(top=0.9) - - if hasattr(targetfig.canvas, "manager"): # Restore the current figure. - _pylab_helpers.Gcf.set_active(targetfig.canvas.manager) - - return SubplotTool(targetfig, toolfig) + with rc_context({"toolbar": "none"}): # No navbar for the toolfig. + # Use new_figure_manager() instead of figure() so that the figure + # doesn't get registered with pyplot. + manager = new_figure_manager(-1, (6, 3)) + manager.set_window_title("Subplot configuration tool") + tool_fig = manager.canvas.figure + tool_fig.subplots_adjust(top=0.9) + manager.show() + return SubplotTool(targetfig, tool_fig) # After deprecation elapses, this can be autogenerated by boilerplate.py. diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index ba0168e48a17..8c95ebd38179 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1099,6 +1099,7 @@ def __init__(self, targetfig, toolfig): The figure instance to embed the subplot tool into. """ + self.figure = toolfig self.targetfig = targetfig toolfig.subplots_adjust(left=0.2, right=0.9) toolfig.suptitle("Click on slider to adjust subplot param") pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy