Skip to content

Commit c55ca9f

Browse files
committed
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.
1 parent 193b8f6 commit c55ca9f

File tree

6 files changed

+16
-68
lines changed

6 files changed

+16
-68
lines changed

lib/matplotlib/backend_bases.py

+5
Original file line numberDiff line numberDiff line change
@@ -3245,6 +3245,11 @@ def _update_view(self):
32453245
ax._set_position(pos_active, 'active')
32463246
self.canvas.draw_idle()
32473247

3248+
def configure_subplots(self, *args):
3249+
from matplotlib import pyplot as plt
3250+
tool = plt.subplot_tool(self.canvas.figure)
3251+
tool.figure.canvas.manager.show()
3252+
32483253
def save_figure(self, *args):
32493254
"""Save the current figure."""
32503255
raise NotImplementedError

lib/matplotlib/backends/_backend_tk.py

-10
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,6 @@ def _Spacer(self):
583583
s.pack(side=tk.LEFT, padx=5)
584584
return s
585585

586-
def configure_subplots(self):
587-
toolfig = Figure(figsize=(6, 3))
588-
window = tk.Toplevel()
589-
canvas = type(self.canvas)(toolfig, master=window)
590-
toolfig.subplots_adjust(top=0.9)
591-
canvas.tool = SubplotTool(self.canvas.figure, toolfig)
592-
canvas.draw()
593-
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
594-
window.grab_set()
595-
596586
def save_figure(self, *args):
597587
filetypes = self.canvas.get_supported_filetypes().copy()
598588
default_filetype = self.canvas.get_default_filetype()

lib/matplotlib/backends/backend_gtk3.py

-28
Original file line numberDiff line numberDiff line change
@@ -587,34 +587,6 @@ def on_notify_filter(*args):
587587
except Exception as e:
588588
error_msg_gtk(str(e), parent=self)
589589

590-
def configure_subplots(self, button):
591-
toolfig = Figure(figsize=(6, 3))
592-
canvas = type(self.canvas)(toolfig)
593-
toolfig.subplots_adjust(top=0.9)
594-
# Need to keep a reference to the tool.
595-
_tool = SubplotTool(self.canvas.figure, toolfig)
596-
597-
w = int(toolfig.bbox.width)
598-
h = int(toolfig.bbox.height)
599-
600-
window = Gtk.Window()
601-
try:
602-
window.set_icon_from_file(window_icon)
603-
except Exception:
604-
# we presumably already logged a message on the
605-
# failure of the main plot, don't keep reporting
606-
pass
607-
window.set_title("Subplot Configuration Tool")
608-
window.set_default_size(w, h)
609-
vbox = Gtk.Box()
610-
vbox.set_property("orientation", Gtk.Orientation.VERTICAL)
611-
window.add(vbox)
612-
vbox.show()
613-
614-
canvas.show()
615-
vbox.pack_start(canvas, True, True, 0)
616-
window.show()
617-
618590
def set_history_buttons(self):
619591
can_backward = self._nav_stack._pos > 0
620592
can_forward = self._nav_stack._pos < len(self._nav_stack._elements) - 1

lib/matplotlib/backends/backend_wx.py

-20
Original file line numberDiff line numberDiff line change
@@ -1186,26 +1186,6 @@ def pan(self, *args):
11861186
self.ToggleTool(self.wx_ids['Zoom'], False)
11871187
NavigationToolbar2.pan(self, *args)
11881188

1189-
def configure_subplots(self, *args):
1190-
global FigureManager # placates pyflakes: created by @_Backend.export
1191-
frame = wx.Frame(None, -1, "Configure subplots")
1192-
_set_frame_icon(frame)
1193-
1194-
toolfig = Figure((6, 3))
1195-
canvas = type(self.canvas)(frame, -1, toolfig)
1196-
1197-
# Create a figure manager to manage things
1198-
FigureManager(canvas, 1, frame)
1199-
1200-
# Now put all into a sizer
1201-
sizer = wx.BoxSizer(wx.VERTICAL)
1202-
# This way of adding to sizer allows resizing
1203-
sizer.Add(canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
1204-
frame.SetSizer(sizer)
1205-
frame.Fit()
1206-
SubplotTool(self.canvas.figure, toolfig)
1207-
frame.Show()
1208-
12091189
def save_figure(self, *args):
12101190
# Fetch the required filename and file type.
12111191
filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards()

lib/matplotlib/pyplot.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1453,19 +1453,19 @@ def subplot_tool(targetfig=None):
14531453
"""
14541454
Launch a subplot tool window for a figure.
14551455
1456-
A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1456+
A `matplotlib.widgets.SubplotTool` instance is returned.
14571457
"""
14581458
if targetfig is None:
14591459
targetfig = gcf()
1460-
1461-
with rc_context({'toolbar': 'None'}): # No nav toolbar for the toolfig.
1462-
toolfig = figure(figsize=(6, 3))
1463-
toolfig.subplots_adjust(top=0.9)
1464-
1465-
if hasattr(targetfig.canvas, "manager"): # Restore the current figure.
1466-
_pylab_helpers.Gcf.set_active(targetfig.canvas.manager)
1467-
1468-
return SubplotTool(targetfig, toolfig)
1460+
with rc_context({"toolbar": "none"}): # No navbar for the toolfig.
1461+
# Use new_figure_manager() instead of figure() so that the figure
1462+
# doesn't get registered with pyplot.
1463+
manager = new_figure_manager(-1, (6, 3))
1464+
manager.set_window_title("Subplot configuration tool")
1465+
tool_fig = manager.canvas.figure
1466+
tool_fig.subplots_adjust(top=0.9)
1467+
manager.show()
1468+
return SubplotTool(targetfig, tool_fig)
14691469

14701470

14711471
# After deprecation elapses, this can be autogenerated by boilerplate.py.

lib/matplotlib/widgets.py

+1
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ def __init__(self, targetfig, toolfig):
10991099
The figure instance to embed the subplot tool into.
11001100
"""
11011101

1102+
self.figure = toolfig
11021103
self.targetfig = targetfig
11031104
toolfig.subplots_adjust(left=0.2, right=0.9)
11041105
toolfig.suptitle("Click on slider to adjust subplot param")

0 commit comments

Comments
 (0)