2yan
2yan

Reputation: 307

Stop Matplotlib Jupyter notebooks from showing plot with animation

The issue I;'m having is that displaying the JavaScript animation in a jupyter notebook shows the plot as well:

Code for the example:

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
        frames=200, interval=20, blit=True)

HTML(anim.to_jshtml())

This is the output:two images

Notice that this results in two plots instead of just the animation.

On a different note I have tried running it with:

HTML(anim.to_html5_video())

but that gives me one of these errors:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Continuum\miniconda3\lib\site-packages\matplotlib\animation.py in __getitem__(self, name)
    169         try:
--> 170             return self.avail[name]
    171         except KeyError:

KeyError: 'ffmpeg'

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
<ipython-input-30-b5253c68f7fe> in <module>()
     20         frames=200, interval=20, blit=True)
     21 
---> 22 HTML(anim.to_html5_video())

~\AppData\Local\Continuum\miniconda3\lib\site-packages\matplotlib\animation.py in to_html5_video(self, embed_limit)
   1347                 # We create a writer manually so that we can get the
   1348                 # appropriate size for the tag
-> 1349                 Writer = writers[rcParams['animation.writer']]
   1350                 writer = Writer(codec='h264',
   1351                                 bitrate=rcParams['animation.bitrate'],

~\AppData\Local\Continuum\miniconda3\lib\site-packages\matplotlib\animation.py in __getitem__(self, name)
    171         except KeyError:
    172             raise RuntimeError(
--> 173                 'Requested MovieWriter ({}) not available'.format(name))
    174 
    175 

RuntimeError: Requested MovieWriter (ffmpeg) not available

and installing ffmpeg is not helping.

Upvotes: 5

Views: 2528

Answers (2)

busybear
busybear

Reputation: 10590

I believe since it's because the notebook is interactive that you are getting a plot automatically without calling plt.show. You can call plt.close to close it manually (or change the interactive mode, but you might want to keep that for other things).

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
plt.close(fig)
...

And I would just make sure ffmpeg is installed correctly to get HTML(anim.to_html5_video()) to work. Seems like Python just can't find it.

Upvotes: 4

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339705

Those are two different issues, right? You may opt for %%captureing the output of the cell and displaying the output in a new cell instead.

enter image description here

As for the missing ffmpeg, you need to make sure that ffmpeg is actually found on your system.

Upvotes: 1

Related Questions