Skip to content

Commit b384e57

Browse files
committed
Simplify animation dont-start-on-save.
Animation connects to draw_event to figure out whether to start the animation, but needs to temporarily disconnect the draw_event handler to avoid triggering the animation start when saving the animation. Instead one can just reuse the canvas._is_saving flag in the draw_event handler to do that.
1 parent 27d1949 commit b384e57

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

lib/matplotlib/animation.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -939,9 +939,11 @@ def _start(self, *args):
939939
Starts interactive animation. Adds the draw frame command to the GUI
940940
handler, calls show to start the event loop.
941941
"""
942+
# Do not start the event source if saving() it.
943+
if self._fig.canvas.is_saving():
944+
return
942945
# First disconnect our draw event handler
943946
self._fig.canvas.mpl_disconnect(self._first_draw_id)
944-
self._first_draw_id = None # So we can check on save
945947

946948
# Now do any initial draw
947949
self._init_draw()
@@ -1057,14 +1059,6 @@ def func(current_frame: int, total_frames: int) -> Any
10571059
if savefig_kwargs is None:
10581060
savefig_kwargs = {}
10591061

1060-
# Need to disconnect the first draw callback, since we'll be doing
1061-
# draws. Otherwise, we'll end up starting the animation.
1062-
if self._first_draw_id is not None:
1063-
self._fig.canvas.mpl_disconnect(self._first_draw_id)
1064-
reconnect_first_draw = True
1065-
else:
1066-
reconnect_first_draw = False
1067-
10681062
if fps is None and hasattr(self, '_interval'):
10691063
# Convert interval in ms to frames per second
10701064
fps = 1000. / self._interval
@@ -1123,8 +1117,11 @@ def func(current_frame: int, total_frames: int) -> Any
11231117
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
11241118
"frame size to vary, which is inappropriate for "
11251119
"animation.")
1120+
# canvas._is_saving = True makes the draw_event animation-starting
1121+
# callback a no-op.
11261122
with mpl.rc_context({'savefig.bbox': None}), \
1127-
writer.saving(self._fig, filename, dpi):
1123+
writer.saving(self._fig, filename, dpi), \
1124+
cbook._setattr_cm(self._fig.canvas, _is_saving=True):
11281125
for anim in all_anim:
11291126
anim._init_draw() # Clear the initial frame
11301127
frame_number = 0
@@ -1145,11 +1142,6 @@ def func(current_frame: int, total_frames: int) -> Any
11451142
frame_number += 1
11461143
writer.grab_frame(**savefig_kwargs)
11471144

1148-
# Reconnect signal for first draw if necessary
1149-
if reconnect_first_draw:
1150-
self._first_draw_id = self._fig.canvas.mpl_connect('draw_event',
1151-
self._start)
1152-
11531145
def _step(self, *args):
11541146
"""
11551147
Handler for getting events. By default, gets the next frame in the

0 commit comments

Comments
 (0)