Skip to content

Commit f320abf

Browse files
committed
Dedupe implementations of configure_subplot().
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.
1 parent 71163e9 commit f320abf

File tree

6 files changed

+16
-69
lines changed

6 files changed

+16
-69
lines changed

lib/matplotlib/backend_bases.py

+5
Original file line numberDiff line numberDiff line change
@@ -3082,6 +3082,11 @@ def _update_view(self):
30823082
ax._set_position(pos_active, 'active')
30833083
self.canvas.draw_idle()
30843084

3085+
def configure_subplots(self, *args):
3086+
from matplotlib import pyplot as plt
3087+
tool = plt.subplot_tool(self.canvas.figure)
3088+
tool.figure.canvas.manager.show()
3089+
30853090
def save_figure(self, *args):
30863091
"""Save the current figure."""
30873092
raise NotImplementedError

lib/matplotlib/backends/_backend_tk.py

-10
Original file line numberDiff line numberDiff line change
@@ -575,16 +575,6 @@ def _init_toolbar(self):
575575
self._message_label = tk.Label(master=self, textvariable=self.message)
576576
self._message_label.pack(side=tk.RIGHT)
577577

578-
def configure_subplots(self):
579-
toolfig = Figure(figsize=(6, 3))
580-
window = tk.Toplevel()
581-
canvas = type(self.canvas)(toolfig, master=window)
582-
toolfig.subplots_adjust(top=0.9)
583-
canvas.tool = SubplotTool(self.canvas.figure, toolfig)
584-
canvas.draw()
585-
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
586-
window.grab_set()
587-
588578
def save_figure(self, *args):
589579
filetypes = self.canvas.get_supported_filetypes().copy()
590580
default_filetype = self.canvas.get_default_filetype()

lib/matplotlib/backends/backend_gtk3.py

-28
Original file line numberDiff line numberDiff line change
@@ -565,34 +565,6 @@ def on_notify_filter(*args):
565565
except Exception as e:
566566
error_msg_gtk(str(e), parent=self)
567567

568-
def configure_subplots(self, button):
569-
toolfig = Figure(figsize=(6, 3))
570-
canvas = type(self.canvas)(toolfig)
571-
toolfig.subplots_adjust(top=0.9)
572-
# Need to keep a reference to the tool.
573-
_tool = SubplotTool(self.canvas.figure, toolfig)
574-
575-
w = int(toolfig.bbox.width)
576-
h = int(toolfig.bbox.height)
577-
578-
window = Gtk.Window()
579-
try:
580-
window.set_icon_from_file(window_icon)
581-
except Exception:
582-
# we presumably already logged a message on the
583-
# failure of the main plot, don't keep reporting
584-
pass
585-
window.set_title("Subplot Configuration Tool")
586-
window.set_default_size(w, h)
587-
vbox = Gtk.Box()
588-
vbox.set_property("orientation", Gtk.Orientation.VERTICAL)
589-
window.add(vbox)
590-
vbox.show()
591-
592-
canvas.show()
593-
vbox.pack_start(canvas, True, True, 0)
594-
window.show()
595-
596568
def set_history_buttons(self):
597569
can_backward = self._nav_stack._pos > 0
598570
can_forward = self._nav_stack._pos < len(self._nav_stack._elements) - 1

lib/matplotlib/backends/backend_wx.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ def _onClose(self, event):
10081008
self.canvas.close_event()
10091009
self.canvas.stop_event_loop()
10101010
Gcf.destroy(self.num)
1011-
# self.Destroy()
1011+
self.Destroy()
10121012

10131013
def GetToolBar(self):
10141014
"""Override wxFrame::GetToolBar as we don't have managed toolbar"""
@@ -1167,26 +1167,6 @@ def pan(self, *args):
11671167
self.ToggleTool(self.wx_ids['Zoom'], False)
11681168
NavigationToolbar2.pan(self, *args)
11691169

1170-
def configure_subplots(self, *args):
1171-
global FigureManager # placates pyflakes: created by @_Backend.export
1172-
frame = wx.Frame(None, -1, "Configure subplots")
1173-
_set_frame_icon(frame)
1174-
1175-
toolfig = Figure((6, 3))
1176-
canvas = type(self.canvas)(frame, -1, toolfig)
1177-
1178-
# Create a figure manager to manage things
1179-
FigureManager(canvas, 1, frame)
1180-
1181-
# Now put all into a sizer
1182-
sizer = wx.BoxSizer(wx.VERTICAL)
1183-
# This way of adding to sizer allows resizing
1184-
sizer.Add(canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
1185-
frame.SetSizer(sizer)
1186-
frame.Fit()
1187-
SubplotTool(self.canvas.figure, toolfig)
1188-
frame.Show()
1189-
11901170
def save_figure(self, *args):
11911171
# Fetch the required filename and file type.
11921172
filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards()

lib/matplotlib/pyplot.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -1279,19 +1279,18 @@ def subplot_tool(targetfig=None):
12791279
"""
12801280
Launch a subplot tool window for a figure.
12811281
1282-
A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1282+
A `matplotlib.widgets.SubplotTool` instance is returned.
12831283
"""
12841284
if targetfig is None:
12851285
targetfig = gcf()
1286-
1287-
with rc_context({'toolbar': 'None'}): # No nav toolbar for the toolfig.
1288-
toolfig = figure(figsize=(6, 3))
1289-
toolfig.subplots_adjust(top=0.9)
1290-
1291-
if hasattr(targetfig.canvas, "manager"): # Restore the current figure.
1292-
_pylab_helpers.Gcf.set_active(targetfig.canvas.manager)
1293-
1294-
return SubplotTool(targetfig, toolfig)
1286+
with rc_context({"toolbar": "none"}): # No navbar for the toolfig.
1287+
# Use new_figure_manager() instead of figure() so that the figure
1288+
# doesn't get registered with pyplot.
1289+
manager = new_figure_manager(-1, (6, 3))
1290+
manager.set_window_title("Subplot configuration tool")
1291+
tool_fig = manager.canvas.figure
1292+
tool_fig.subplots_adjust(top=0.9)
1293+
return SubplotTool(targetfig, tool_fig)
12951294

12961295

12971296
def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):

lib/matplotlib/widgets.py

+1
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ def __init__(self, targetfig, toolfig):
11181118
The figure instance to embed the subplot tool into.
11191119
"""
11201120

1121+
self.figure = toolfig
11211122
self.targetfig = targetfig
11221123
toolfig.subplots_adjust(left=0.2, right=0.9)
11231124
toolfig.suptitle("Click on slider to adjust subplot param")

0 commit comments

Comments
 (0)