From 0e2935e28c0b9b528655ef3af1b866442cd67c97 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 22 May 2022 19:51:24 +0200 Subject: [PATCH] Remove custom backend_nbagg.show(), putting logic in manager show. The _Backend class machinery can autogenerate a backend_module.show() function, which (in short) calls manager.show() on all managers and then calls the optionally-provided mainloop() function. backend_nbagg currently completely bypasses this autogeneration because it wants to additionally fiddle with callbacks and Gcf (it doesn't need any mainloop()), but that logic can just as well go to FigureManagerNbAgg.show() instead. This way it benefits from the default-generated show; moreover, it seems a bit strange that figures shown with manager.show() (instead of plt.show()) do indeed get displayed, but just don't have their callbacks/Gcf-ness adjusted. This is also related to the move towards putting show()-generating information on the canvas class for inheritability. --- lib/matplotlib/backends/backend_nbagg.py | 33 +++++++----------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/lib/matplotlib/backends/backend_nbagg.py b/lib/matplotlib/backends/backend_nbagg.py index 1ee2911a8b39..712f45735940 100644 --- a/lib/matplotlib/backends/backend_nbagg.py +++ b/lib/matplotlib/backends/backend_nbagg.py @@ -96,6 +96,15 @@ def show(self): else: self.canvas.draw_idle() self._shown = True + # plt.figure adds an event which makes the figure in focus the active + # one. Disable this behaviour, as it results in figures being put as + # the active figure after they have been shown, even in non-interactive + # mode. + if hasattr(self, '_cidgcf'): + self.canvas.mpl_disconnect(self._cidgcf) + if not is_interactive(): + from matplotlib._pylab_helpers import Gcf + Gcf.figs.pop(self.num, None) def reshow(self): """ @@ -232,27 +241,3 @@ def on_message(self, message): class _BackendNbAgg(_Backend): FigureCanvas = FigureCanvasNbAgg FigureManager = FigureManagerNbAgg - - @staticmethod - def show(block=None): - ## TODO: something to do when keyword block==False ? - from matplotlib._pylab_helpers import Gcf - - managers = Gcf.get_all_fig_managers() - if not managers: - return - - interactive = is_interactive() - - for manager in managers: - manager.show() - - # plt.figure adds an event which makes the figure in focus the - # active one. Disable this behaviour, as it results in - # figures being put as the active figure after they have been - # shown, even in non-interactive mode. - if hasattr(manager, '_cidgcf'): - manager.canvas.mpl_disconnect(manager._cidgcf) - - if not interactive: - Gcf.figs.pop(manager.num, None)