@@ -2840,6 +2840,53 @@ def create_with_canvas(cls, canvas_class, figure, num):
2840
2840
"""
2841
2841
return cls (canvas_class (figure ), num )
2842
2842
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
+
2843
2890
def show (self ):
2844
2891
"""
2845
2892
For GUI backends, show the figure window and redraw.
@@ -3518,7 +3565,12 @@ def new_figure_manager_given_figure(cls, num, figure):
3518
3565
3519
3566
@classmethod
3520
3567
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 ():
3522
3574
manager = Gcf .get_active ()
3523
3575
if manager :
3524
3576
manager .canvas .draw_idle ()
@@ -3546,8 +3598,8 @@ def show(cls, *, block=None):
3546
3598
# Hack: Are we in IPython's %pylab mode? In pylab mode, IPython
3547
3599
# (>= 0.10) tacks a _needmain attribute onto pyplot.show (always
3548
3600
# 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" )
3551
3603
block = not ipython_pylab and not is_interactive ()
3552
3604
if block :
3553
3605
cls .mainloop ()
0 commit comments