Aug-15-2024, 09:12 AM
Hi. I was trying to follow the code in a book to generate an animation in Jupyter Lab. However, the system shows 'RuntimeError'. I have already installed ffmpeg.exe in the folder, so I am not sure why I get the runtime error. Any help is appreciated.
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from IPython.display import HTML x = np.linspace(0, 2*np.pi, 100) y = x* np.sin(2*x) fig, ax = plt.subplots() dot,=ax.plot([],[],'ro',markersize=12) plt.close() def init(): ax.plot(x,y) def animate(i): dot.set_data(x[i],y[i]) ani=FuncAnimation(fig=fig, func=animate, frames=100, init_func=init, interval=50) HTML(ani.to_html5_video())
Error:RuntimeError Traceback (most recent call last)
Cell In[3], line 18
15 dot.set_data(x[i],y[i])
17 ani=FuncAnimation(fig=fig, func=animate, frames=100, init_func=init, interval=50)
---> 18 HTML(ani.to_html5_video())
File C:\myWork\myVenv\Lib\site-packages\matplotlib\animation.py:1289, in Animation.to_html5_video(self, embed_limit)
1285 Writer = writers[mpl.rcParams['animation.writer']]
1286 writer = Writer(codec='h264',
1287 bitrate=mpl.rcParams['animation.bitrate'],
1288 fps=1000. / self._interval)
-> 1289 self.save(str(path), writer=writer)
1290 # Now open and base64 encode.
1291 vid64 = base64.encodebytes(path.read_bytes())
File C:\myWork\myVenv\Lib\site-packages\matplotlib\animation.py:1105, in Animation.save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs, progress_callback)
1102 for data in zip(*[a.new_saved_frame_seq() for a in all_anim]):
1103 for anim, d in zip(all_anim, data):
1104 # TODO: See if turning off blit is really necessary
-> 1105 anim._draw_next_frame(d, blit=False)
1106 if progress_callback is not None:
1107 progress_callback(frame_number, total_frames)
File C:\myWork\myVenv\Lib\site-packages\matplotlib\animation.py:1140, in Animation._draw_next_frame(self, framedata, blit)
1136 def _draw_next_frame(self, framedata, blit):
1137 # Breaks down the drawing of the next frame into steps of pre- and
1138 # post- draw, as well as the drawing of the frame itself.
1139 self._pre_draw(framedata, blit)
-> 1140 self._draw_frame(framedata)
1141 self._post_draw(framedata, blit)
File C:\myWork\myVenv\Lib\site-packages\matplotlib\animation.py:1766, in FuncAnimation._draw_frame(self, framedata)
1762 self._save_seq = self._save_seq[-self._save_count:]
1764 # Call the func with framedata and args. If blitting is desired,
1765 # func needs to return a sequence of any artists that were modified.
-> 1766 self._drawn_artists = self._func(framedata, *self._args)
1768 if self._blit:
1770 err = RuntimeError('The animation function must return a sequence '
1771 'of Artist objects.')
Cell In[3], line 15, in animate(i)
14 def animate(i):
---> 15 dot.set_data(x[i],y[i])
File C:\myWork\myVenv\Lib\site-packages\matplotlib\lines.py:665, in Line2D.set_data(self, *args)
662 else:
663 x, y = args
--> 665 self.set_xdata(x)
666 self.set_ydata(y)
File C:\myWork\myVenv\Lib\site-packages\matplotlib\lines.py:1289, in Line2D.set_xdata(self, x)
1276 """
1277 Set the data array for x.
1278
(...)
1286 set_ydata
1287 """
1288 if not np.iterable(x):
-> 1289 raise RuntimeError('x must be a sequence')
1290 self._xorig = copy.copy(x)
1291 self._invalidx = True
RuntimeError: x must be a sequence