Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132923 views
License: OTHER
Kernel: Python 3
from __future__ import print_function, division %matplotlib inline import numpy as np import matplotlib.pyplot as plt from matplotlib import animation from matplotlib import rc # this notebook works with rc('animation', html='jshtml') # and until a recent update, it also worked with rc('animation', html='html5') # but now it produces the error below
N = 10 array = np.zeros((N, N))
def init_func(): """Called at the beginning of an animation.""" pass def animate_func(i): """Draws one frame of the animation.""" a = np.zeros((N, N)) a[i, i] = 1 image.set_array(a) return (image,)
fig = plt.gcf() a = np.zeros((N, N)) a[0, 0] = 1 image = plt.imshow(a) anim = animation.FuncAnimation(fig, animate_func, init_func=init_func, frames=N, interval=20)
Image in a Jupyter notebook
anim
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj) 343 method = get_real_method(obj, self.print_method) 344 if method is not None: --> 345 return method() 346 return None 347 else: ~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in _repr_html_(self) 1482 fmt = rcParams['animation.html'] 1483 if fmt == 'html5': -> 1484 return self.to_html5_video() 1485 elif fmt == 'jshtml': 1486 return self.to_jshtml() ~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in to_html5_video(self, embed_limit) 1411 bitrate=rcParams['animation.bitrate'], 1412 fps=1000. / self._interval) -> 1413 self.save(f.name, writer=writer) 1414 1415 # Now open and base64 encode ~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs) 1258 # TODO: See if turning off blit is really necessary 1259 anim._draw_next_frame(d, blit=False) -> 1260 writer.grab_frame(**savefig_kwargs) 1261 1262 # Reconnect signal for first draw if necessary ~/anaconda3/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback) 97 value = type() 98 try: ---> 99 self.gen.throw(type, value, traceback) 100 except StopIteration as exc: 101 # Suppress StopIteration *unless* it's the same exception that ~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in saving(self, fig, outfile, dpi, *args, **kwargs) 235 yield self 236 finally: --> 237 self.finish() 238 239 ~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in finish(self) 367 def finish(self): 368 '''Finish any processing for writing the movie.''' --> 369 self.cleanup() 370 371 def grab_frame(self, **savefig_kwargs): ~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in cleanup(self) 406 def cleanup(self): 407 '''Clean-up and collect the process used to write the movie file.''' --> 408 out, err = self._proc.communicate() 409 self._frame_sink().close() 410 verbose.report('MovieWriter -- ' ~/anaconda3/lib/python3.6/subprocess.py in communicate(self, input, timeout) 841 842 try: --> 843 stdout, stderr = self._communicate(input, endtime, timeout) 844 finally: 845 self._communication_started = True ~/anaconda3/lib/python3.6/subprocess.py in _communicate(self, input, endtime, orig_timeout) 1503 selector.register(self.stdin, selectors.EVENT_WRITE) 1504 if self.stdout: -> 1505 selector.register(self.stdout, selectors.EVENT_READ) 1506 if self.stderr: 1507 selector.register(self.stderr, selectors.EVENT_READ) ~/anaconda3/lib/python3.6/selectors.py in register(self, fileobj, events, data) 349 350 def register(self, fileobj, events, data=None): --> 351 key = super().register(fileobj, events, data) 352 poll_events = 0 353 if events & EVENT_READ: ~/anaconda3/lib/python3.6/selectors.py in register(self, fileobj, events, data) 235 raise ValueError("Invalid events: {!r}".format(events)) 236 --> 237 key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data) 238 239 if key.fd in self._fd_to_key: ~/anaconda3/lib/python3.6/selectors.py in _fileobj_lookup(self, fileobj) 222 """ 223 try: --> 224 return _fileobj_to_fd(fileobj) 225 except ValueError: 226 # Do an exhaustive search. ~/anaconda3/lib/python3.6/selectors.py in _fileobj_to_fd(fileobj) 37 except (AttributeError, TypeError, ValueError): 38 raise ValueError("Invalid file object: " ---> 39 "{!r}".format(fileobj)) from None 40 if fd < 0: 41 raise ValueError("Invalid file descriptor: {}".format(fd)) ValueError: Invalid file object: <_io.BufferedReader name=49>
<matplotlib.animation.FuncAnimation at 0x7f4f0f772390>