Skip to content

Commit bcc88d3

Browse files
authored
Merge pull request #16818 from anntzer/subplottool
Dedupe implementations of configure_subplots().
2 parents 6e4295c + c55ca9f commit bcc88d3

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
@@ -3268,6 +3268,11 @@ def _update_view(self):
32683268
ax._set_position(pos_active, 'active')
32693269
self.canvas.draw_idle()
32703270

3271+
def configure_subplots(self, *args):
3272+
from matplotlib import pyplot as plt
3273+
tool = plt.subplot_tool(self.canvas.figure)
3274+
tool.figure.canvas.manager.show()
3275+
32713276
def save_figure(self, *args):
32723277
"""Save the current figure."""
32733278
raise NotImplementedError

lib/matplotlib/backends/_backend_tk.py

-10
Original file line numberDiff line numberDiff line change
@@ -595,16 +595,6 @@ def _Spacer(self):
595595
s.pack(side=tk.LEFT, padx=5)
596596
return s
597597

598-
def configure_subplots(self):
599-
toolfig = Figure(figsize=(6, 3))
600-
window = tk.Toplevel()
601-
canvas = type(self.canvas)(toolfig, master=window)
602-
toolfig.subplots_adjust(top=0.9)
603-
canvas.tool = SubplotTool(self.canvas.figure, toolfig)
604-
canvas.draw()
605-
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
606-
window.grab_set()
607-
608598
def save_figure(self, *args):
609599
filetypes = self.canvas.get_supported_filetypes().copy()
610600
default_filetype = self.canvas.get_default_filetype()

lib/matplotlib/backends/backend_gtk3.py

-28
Original file line numberDiff line numberDiff line change
@@ -625,34 +625,6 @@ def on_notify_filter(*args):
625625
except Exception as e:
626626
error_msg_gtk(str(e), parent=self)
627627

628-
def configure_subplots(self, button):
629-
toolfig = Figure(figsize=(6, 3))
630-
canvas = type(self.canvas)(toolfig)
631-
toolfig.subplots_adjust(top=0.9)
632-
# Need to keep a reference to the tool.
633-
_tool = SubplotTool(self.canvas.figure, toolfig)
634-
635-
w = int(toolfig.bbox.width)
636-
h = int(toolfig.bbox.height)
637-
638-
window = Gtk.Window()
639-
try:
640-
window.set_icon_from_file(window_icon)
641-
except Exception:
642-
# we presumably already logged a message on the
643-
# failure of the main plot, don't keep reporting
644-
pass
645-
window.set_title("Subplot Configuration Tool")
646-
window.set_default_size(w, h)
647-
vbox = Gtk.Box()
648-
vbox.set_property("orientation", Gtk.Orientation.VERTICAL)
649-
window.add(vbox)
650-
vbox.show()
651-
652-
canvas.show()
653-
vbox.pack_start(canvas, True, True, 0)
654-
window.show()
655-
656628
def set_history_buttons(self):
657629
can_backward = self._nav_stack._pos > 0
658630
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
@@ -1191,26 +1191,6 @@ def pan(self, *args):
11911191
self.ToggleTool(self.wx_ids['Zoom'], False)
11921192
NavigationToolbar2.pan(self, *args)
11931193

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

lib/matplotlib/pyplot.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1546,19 +1546,19 @@ def subplot_tool(targetfig=None):
15461546
"""
15471547
Launch a subplot tool window for a figure.
15481548
1549-
A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1549+
A `matplotlib.widgets.SubplotTool` instance is returned.
15501550
"""
15511551
if targetfig is None:
15521552
targetfig = gcf()
1553-
1554-
with rc_context({'toolbar': 'None'}): # No nav toolbar for the toolfig.
1555-
toolfig = figure(figsize=(6, 3))
1556-
toolfig.subplots_adjust(top=0.9)
1557-
1558-
if hasattr(targetfig.canvas, "manager"): # Restore the current figure.
1559-
_pylab_helpers.Gcf.set_active(targetfig.canvas.manager)
1560-
1561-
return SubplotTool(targetfig, toolfig)
1553+
with rc_context({"toolbar": "none"}): # No navbar for the toolfig.
1554+
# Use new_figure_manager() instead of figure() so that the figure
1555+
# doesn't get registered with pyplot.
1556+
manager = new_figure_manager(-1, (6, 3))
1557+
manager.set_window_title("Subplot configuration tool")
1558+
tool_fig = manager.canvas.figure
1559+
tool_fig.subplots_adjust(top=0.9)
1560+
manager.show()
1561+
return SubplotTool(targetfig, tool_fig)
15621562

15631563

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

lib/matplotlib/widgets.py

+1
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ def __init__(self, targetfig, toolfig):
11031103
The figure instance to embed the subplot tool into.
11041104
"""
11051105

1106+
self.figure = toolfig
11061107
self.targetfig = targetfig
11071108
toolfig.subplots_adjust(left=0.2, right=0.9)
11081109
toolfig.suptitle("Click on slider to adjust subplot param")

0 commit comments

Comments
 (0)