From 7c48cbd1ffa531567d9c6c0dacbb28601d26b695 Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Wed, 19 Aug 2020 18:52:47 -0400 Subject: [PATCH 1/4] allow Gcf to destroy unknown managers --- lib/matplotlib/_pylab_helpers.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/_pylab_helpers.py b/lib/matplotlib/_pylab_helpers.py index 26ba7a171244..27904dd84b6f 100644 --- a/lib/matplotlib/_pylab_helpers.py +++ b/lib/matplotlib/_pylab_helpers.py @@ -53,18 +53,17 @@ def destroy(cls, num): It is recommended to pass a manager instance, to avoid confusion when two managers share the same number. """ - if all(hasattr(num, attr) for attr in ["num", "_cidgcf", "destroy"]): + if all(hasattr(num, attr) for attr in ["num", "destroy"]): manager = num if cls.figs.get(manager.num) is manager: cls.figs.pop(manager.num) - else: - return else: try: manager = cls.figs.pop(num) except KeyError: return - manager.canvas.mpl_disconnect(manager._cidgcf) + if hasattr(manager, "_cidgcf"): + manager.canvas.mpl_disconnect(manager._cidgcf) manager.destroy() gc.collect(1) From f61ac0722394c38458d775e15298b24eed9d0e9f Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Wed, 19 Aug 2020 18:53:30 -0400 Subject: [PATCH 2/4] keep a reference to the output of subplot_tool to preserve callback weakrefs --- lib/matplotlib/backend_bases.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index fe8cc66913cc..816db303ff65 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3294,8 +3294,8 @@ def _update_view(self): def configure_subplots(self, *args): plt = _safe_pyplot_import() - tool = plt.subplot_tool(self.canvas.figure) - tool.figure.canvas.manager.show() + self.subplot_tool = plt.subplot_tool(self.canvas.figure) + self.subplot_tool.figure.canvas.manager.show() def save_figure(self, *args): """Save the current figure.""" From 673afe7ed3751a97fc5970c3e22a120cc0f5c6c4 Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Thu, 20 Aug 2020 07:58:21 -0400 Subject: [PATCH 3/4] document reference requirement --- lib/matplotlib/pyplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 18e1739cab6d..73e77f6f0174 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -1546,7 +1546,8 @@ def subplot_tool(targetfig=None): """ Launch a subplot tool window for a figure. - A `matplotlib.widgets.SubplotTool` instance is returned. + A `matplotlib.widgets.SubplotTool` instance is returned. You must maintain + a reference to the instance to keep the associated callbacks alive. """ if targetfig is None: targetfig = gcf() From 2c9a85bae564a20104027d5057e5dfcec291c5d9 Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Thu, 20 Aug 2020 09:05:59 -0400 Subject: [PATCH 4/4] solve nbagg recursion bug within nbagg --- lib/matplotlib/backends/backend_nbagg.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_nbagg.py b/lib/matplotlib/backends/backend_nbagg.py index 9eb076d9ec44..b921aa0a1d6e 100644 --- a/lib/matplotlib/backends/backend_nbagg.py +++ b/lib/matplotlib/backends/backend_nbagg.py @@ -231,7 +231,12 @@ def new_figure_manager_given_figure(num, figure): if is_interactive(): manager.show() figure.canvas.draw_idle() - canvas.mpl_connect('close_event', lambda event: Gcf.destroy(manager)) + + def destroy(event): + canvas.mpl_disconnect(cid) + Gcf.destroy(manager) + + cid = canvas.mpl_connect('close_event', destroy) return manager @staticmethod