@@ -2840,6 +2840,53 @@ def create_with_canvas(cls, canvas_class, figure, num):
28402840 """
28412841 return cls (canvas_class (figure ), num )
28422842
2843+ @classmethod
2844+ def start_main_loop (cls ):
2845+ """
2846+ Start the main event loop.
2847+
2848+ This method is called by `.FigureManagerBase.pyplot_show`, which is the
2849+ implementation of `.pyplot.show`. To customize the behavior of
2850+ `.pyplot.show`, interactive backends should usually override
2851+ `~.FigureManagerBase.start_main_loop`; if more customized logic is
2852+ necessary, `~.FigureManagerBase.pyplot_show` can also be overridden.
2853+ """
2854+
2855+ @classmethod
2856+ def pyplot_show (cls , * , block = None ):
2857+ """
2858+ Show all figures. This method is the implementation of `.pyplot.show`.
2859+
2860+ To customize the behavior of `.pyplot.show`, interactive backends
2861+ should usually override `~.FigureManagerBase.start_main_loop`; if more
2862+ customized logic is necessary, `~.FigureManagerBase.pyplot_show` can
2863+ also be overridden.
2864+
2865+ Parameters
2866+ ----------
2867+ block : bool, optional
2868+ Whether to block by calling ``start_main_loop``. The default,
2869+ None, means to block if we are neither in IPython's ``%pylab`` mode
2870+ nor in ``interactive`` mode.
2871+ """
2872+ managers = Gcf .get_all_fig_managers ()
2873+ if not managers :
2874+ return
2875+ for manager in managers :
2876+ try :
2877+ manager .show () # Emits a warning for non-interactive backend.
2878+ except NonGuiException as exc :
2879+ _api .warn_external (str (exc ))
2880+ if block is None :
2881+ # Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
2882+ # (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
2883+ # set to False).
2884+ ipython_pylab = hasattr (
2885+ getattr (sys .modules .get ("pyplot" ), "show" , None ), "_needmain" )
2886+ block = not ipython_pylab and not is_interactive ()
2887+ if block :
2888+ cls .start_main_loop ()
2889+
28432890 def show (self ):
28442891 """
28452892 For GUI backends, show the figure window and redraw.
@@ -3518,7 +3565,12 @@ def new_figure_manager_given_figure(cls, num, figure):
35183565
35193566 @classmethod
35203567 def draw_if_interactive (cls ):
3521- if cls .mainloop is not None and is_interactive ():
3568+ manager_class = cls .FigureCanvas .manager_class
3569+ # Interactive backends reimplement start_main_loop or pyplot_show.
3570+ backend_is_interactive = (
3571+ manager_class .start_main_loop != FigureManagerBase .start_main_loop
3572+ or manager_class .pyplot_show != FigureManagerBase .pyplot_show )
3573+ if backend_is_interactive and is_interactive ():
35223574 manager = Gcf .get_active ()
35233575 if manager :
35243576 manager .canvas .draw_idle ()
@@ -3546,8 +3598,8 @@ def show(cls, *, block=None):
35463598 # Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
35473599 # (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
35483600 # set to False).
3549- from matplotlib import pyplot
3550- ipython_pylab = hasattr ( pyplot . show , "_needmain" )
3601+ ipython_pylab = hasattr (
3602+ getattr ( sys . modules . get ( "pyplot" ), " show" , None ) , "_needmain" )
35513603 block = not ipython_pylab and not is_interactive ()
35523604 if block :
35533605 cls .mainloop ()
0 commit comments