Skip to content

Commit 73d1af2

Browse files
committed
Move draw_if_interactive logic to new_figure_manager_given_figure.
Currently, Matplotlib only ever calls draw_if_interactive at the end of `plt.figure()` (and upon figure unpickling), i.e. this is a mechanism to further customize handling of newly generated figures. (Prior to Matplotlib 1.5, draw_if_interactive was also called at the end of all pyplot functions to trigger a figure redraw, but this is now handled by the stale attribute.) In order to simplify the backend API ("what is the API that a backend module must/can provide", I am planning to deprecate (on Matplotlib's side) the ability for backends to provide a draw_if_interactive function (forcing them to always do `if interactive(): draw_idle()`). Instead, any relevant new-figure-customization logic can instead go into `new_figure_manager_given_figure`. This PR implements this change for ipympl, and should be fully back-compatible all the way back to Matplotlib 1.5. I would like to make these changes first on the side of the clients (i.e., the third-party backends), to catch any possible problems with the intended change on Matplotlib's side.
1 parent 913431e commit 73d1af2

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

ipympl/backend_nbagg.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,21 @@ def destroy(event):
495495
Gcf.destroy(manager)
496496

497497
cid = canvas.mpl_connect('close_event', destroy)
498+
499+
# Only register figure for showing when in interactive mode (otherwise
500+
# we'll generate duplicate plots, since a user who set ioff() manually
501+
# expects to make separate draw/show calls).
502+
if is_interactive():
503+
# ensure current figure will be drawn.
504+
try:
505+
_Backend_ipympl._to_show.remove(figure)
506+
except ValueError:
507+
# ensure it only appears in the draw list once
508+
pass
509+
# Queue up the figure for drawing in next show() call
510+
_Backend_ipympl._to_show.append(figure)
511+
_Backend_ipympl._draw_called = True
512+
498513
return manager
499514

500515
@staticmethod
@@ -523,32 +538,6 @@ def show(block=None):
523538
if manager.canvas.figure in _Backend_ipympl._to_show:
524539
_Backend_ipympl._to_show.remove(manager.canvas.figure)
525540

526-
@staticmethod
527-
def draw_if_interactive():
528-
# If matplotlib was manually set to non-interactive mode, this function
529-
# should be a no-op (otherwise we'll generate duplicate plots, since a
530-
# user who set ioff() manually expects to make separate draw/show
531-
# calls).
532-
if not is_interactive():
533-
return
534-
535-
manager = Gcf.get_active()
536-
if manager is None:
537-
return
538-
fig = manager.canvas.figure
539-
540-
# ensure current figure will be drawn, and each subsequent call
541-
# of draw_if_interactive() moves the active figure to ensure it is
542-
# drawn last
543-
try:
544-
_Backend_ipympl._to_show.remove(fig)
545-
except ValueError:
546-
# ensure it only appears in the draw list once
547-
pass
548-
# Queue up the figure for drawing in next show() call
549-
_Backend_ipympl._to_show.append(fig)
550-
_Backend_ipympl._draw_called = True
551-
552541

553542
def flush_figures():
554543
if rcParams['backend'] == 'module://ipympl.backend_nbagg':

0 commit comments

Comments
 (0)