@@ -2848,6 +2848,53 @@ def create_with_canvas(cls, canvas_class, figure, num):
28482848 """
28492849 return cls (canvas_class (figure ), num )
28502850
2851+ @classmethod
2852+ def start_main_loop (cls ):
2853+ """
2854+ Start the main event loop.
2855+
2856+ This method is called by `.FigureManagerBase.pyplot_show`, which is the
2857+ implementation of `.pyplot.show`. To customize the behavior of
2858+ `.pyplot.show`, interactive backends should usually override
2859+ `~.FigureManagerBase.start_main_loop`; if more customized logic is
2860+ necessary, `~.FigureManagerBase.pyplot_show` can also be overridden.
2861+ """
2862+
2863+ @classmethod
2864+ def pyplot_show (cls , * , block = None ):
2865+ """
2866+ Show all figures. This method is the implementation of `.pyplot.show`.
2867+
2868+ To customize the behavior of `.pyplot.show`, interactive backends
2869+ should usually override `~.FigureManagerBase.start_main_loop`; if more
2870+ customized logic is necessary, `~.FigureManagerBase.pyplot_show` can
2871+ also be overridden.
2872+
2873+ Parameters
2874+ ----------
2875+ block : bool, optional
2876+ Whether to block by calling ``start_main_loop``. The default,
2877+ None, means to block if we are neither in IPython's ``%pylab`` mode
2878+ nor in ``interactive`` mode.
2879+ """
2880+ managers = Gcf .get_all_fig_managers ()
2881+ if not managers :
2882+ return
2883+ for manager in managers :
2884+ try :
2885+ manager .show () # Emits a warning for non-interactive backend.
2886+ except NonGuiException as exc :
2887+ _api .warn_external (str (exc ))
2888+ if block is None :
2889+ # Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
2890+ # (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
2891+ # set to False).
2892+ ipython_pylab = hasattr (
2893+ getattr (sys .modules .get ("pyplot" ), "show" , None ), "_needmain" )
2894+ block = not ipython_pylab and not is_interactive ()
2895+ if block :
2896+ cls .start_main_loop ()
2897+
28512898 def show (self ):
28522899 """
28532900 For GUI backends, show the figure window and redraw.
@@ -3526,7 +3573,12 @@ def new_figure_manager_given_figure(cls, num, figure):
35263573
35273574 @classmethod
35283575 def draw_if_interactive (cls ):
3529- if cls .mainloop is not None and is_interactive ():
3576+ manager_class = cls .FigureCanvas .manager_class
3577+ # Interactive backends reimplement start_main_loop or pyplot_show.
3578+ backend_is_interactive = (
3579+ manager_class .start_main_loop != FigureManagerBase .start_main_loop
3580+ or manager_class .pyplot_show != FigureManagerBase .pyplot_show )
3581+ if backend_is_interactive and is_interactive ():
35303582 manager = Gcf .get_active ()
35313583 if manager :
35323584 manager .canvas .draw_idle ()
@@ -3554,8 +3606,8 @@ def show(cls, *, block=None):
35543606 # Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
35553607 # (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
35563608 # set to False).
3557- from matplotlib import pyplot
3558- ipython_pylab = hasattr ( pyplot . show , "_needmain" )
3609+ ipython_pylab = hasattr (
3610+ getattr ( sys . modules . get ( "pyplot" ), " show" , None ) , "_needmain" )
35593611 block = not ipython_pylab and not is_interactive ()
35603612 if block :
35613613 cls .mainloop ()
0 commit comments