Skip to content

Commit 58eda85

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 95ea60c commit 58eda85

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
@@ -922,9 +922,11 @@ def _start(self, *args):
922922
Starts interactive animation. Adds the draw frame command to the GUI
923923
handler, calls show to start the event loop.
924924
'''
925+
# Do not start the event source if saving() it.
926+
if self._fig.canvas.is_saving():
927+
return
925928
# First disconnect our draw event handler
926929
self._fig.canvas.mpl_disconnect(self._first_draw_id)
927-
self._first_draw_id = None # So we can check on save
928930

929931
# Now do any initial draw
930932
self._init_draw()
@@ -1040,14 +1042,6 @@ def func(current_frame: int, total_frames: int) -> Any
10401042
if savefig_kwargs is None:
10411043
savefig_kwargs = {}
10421044

1043-
# Need to disconnect the first draw callback, since we'll be doing
1044-
# draws. Otherwise, we'll end up starting the animation.
1045-
if self._first_draw_id is not None:
1046-
self._fig.canvas.mpl_disconnect(self._first_draw_id)
1047-
reconnect_first_draw = True
1048-
else:
1049-
reconnect_first_draw = False
1050-
10511045
if fps is None and hasattr(self, '_interval'):
10521046
# Convert interval in ms to frames per second
10531047
fps = 1000. / self._interval
@@ -1106,8 +1100,11 @@ def func(current_frame: int, total_frames: int) -> Any
11061100
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
11071101
"frame size to vary, which is inappropriate for "
11081102
"animation.")
1103+
# canvas._is_saving = True makes the draw_event animation-starting
1104+
# callback a no-op.
11091105
with mpl.rc_context({'savefig.bbox': None}), \
1110-
writer.saving(self._fig, filename, dpi):
1106+
writer.saving(self._fig, filename, dpi), \
1107+
cbook._setattr_cm(self._fig.canvas, _is_saving=True):
11111108
for anim in all_anim:
11121109
anim._init_draw() # Clear the initial frame
11131110
frame_number = 0
@@ -1128,11 +1125,6 @@ def func(current_frame: int, total_frames: int) -> Any
11281125
frame_number += 1
11291126
writer.grab_frame(**savefig_kwargs)
11301127

1131-
# Reconnect signal for first draw if necessary
1132-
if reconnect_first_draw:
1133-
self._first_draw_id = self._fig.canvas.mpl_connect('draw_event',
1134-
self._start)
1135-
11361128
def _step(self, *args):
11371129
'''
11381130
Handler for getting events. By default, gets the next frame in the

0 commit comments

Comments
 (0)