Skip to content

Commit

Permalink
reduce number of draws to increase speed (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhi authored Aug 18, 2023
1 parent 06a7faf commit 0a3fb33
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions mpl_playback/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import Literal
from unittest import mock
from contextlib import contextmanager

import matplotlib
import numpy as np
Expand Down Expand Up @@ -219,12 +220,26 @@ def playback_events(
if prog_bar and _prog_bar:
pbar = tqdm(total=N_frames)

def fake_draw(*args, **kwargs):
pass

@contextmanager
def patch_draw(fig):
orig_draw = fig.canvas.draw
try:
fig.canvas.draw = fake_draw
yield
finally:
fig.canvas.draw = orig_draw

def animate(i):
idx = event_frames == i
if np.sum(idx) != 0:
for event in mock_events[idx]:
# event = mock_events[i]
accessors[event._figname].canvas.callbacks.process(event.name, event)
fig = accessors[event._figname]
with patch_draw(fig):
fig.canvas.callbacks.process(event.name, event)
if event.name == "motion_notify_event":
# now set the cursor invisible so multiple don't show up
# if there are multiple figures
Expand All @@ -237,16 +252,10 @@ def animate(i):
# transform is crucial or else the cursor will show up in a weirdly
# scaled position. This is true even with setting `transform=None`
# on the origin `plot` call
f = _figs[event._figname]
xy = transforms[event._figname].transform([event.x, event.y])
fake_cursors[event._figname].set_data([xy[0]], [xy[1]])
fake_cursors[event._figname].set_visible(True)

# theres got to be a clever way to avoid doing these gazillion draws
# maybe monkeypatching the figure's draw event?
for f in _figs.values():
f.canvas.draw()

if prog_bar and _prog_bar:
pbar.update(1)
if outputs is not None:
Expand Down

0 comments on commit 0a3fb33

Please sign in to comment.