-
Notifications
You must be signed in to change notification settings - Fork 23
/
util_matplotlib.py
38 lines (33 loc) · 992 Bytes
/
util_matplotlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import base64
import contextlib
import io
from IPython.display import clear_output, display_html, HTML
import time
import urllib
class SimpleMovieWriter(object):
"""
Usage example:
anim = animation.FuncAnimation(...)
anim.save(None, writer=SimpleMovieWriter(sleep=0.01))
"""
def __init__(self, sleep=0.1):
self.sleep = sleep
self.fig = None
def setup(self, fig):
self.fig = fig
def grab_frame(self, **kwargs):
img_data = io.BytesIO()
self.fig.savefig(img_data, format='jpeg')
img_data.seek(0)
uri = 'data:image/jpeg;base64,' + urllib.request.quote(base64.b64encode(img_data.getbuffer()))
img_data.close()
clear_output(wait=True)
display_html(HTML('<img src="' + uri + '">'))
time.sleep(self.sleep)
@contextlib.contextmanager
def saving(self, fig, *args, **kwargs):
self.setup(fig)
try:
yield self
finally:
pass