Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 253
2yan Avatar asked Jul 06 '18 15:07

2yan


People also ask

How do I stop the plot showing in matplotlib?

We can simply save plots generated from Matplotlib using savefig() and imsave() methods. If we are in interactive mode, the plot might get displayed. To avoid the display of plot we use close() and ioff() methods.

How do you show animations in a jupyter notebook?

What we can do is convert the animation into an HTML5 video and embed it in Jupyter Notebook. We will be using FFmpeg for conversion. If you don't have it, you first need to follow the instruction to download FFmpeg and unzip it. Creating animation is the same as the previous example.

Why is %Matplotlib inline?

Why matplotlib inline is used. You can use the magic function %matplotlib inline to enable the inline plotting, where the plots/graphs will be displayed just below the cell where your plotting commands are written. It provides interactivity with the backend in the frontends like the jupyter notebook.

Is PLT show necessary in Jupyter?

Plotting from an IPython shell Using plt. show() in Matplotlib mode is not required.

How to create an interactive plot in Jupyter Notebook?

Interactive Plot in Jupyter Notebook In order to create an interactive plot in Jupyter Notebook, you first need to enable interactive plot as follows: After that, we import the required libraries. Especially FuncAnimation class that can be used to create an animation for you. Next, we need to create an initial state of the animation figure.

How do you use matplotlib for animation?

Using Matplotlib for Animations. Matplotlib library of Python is a plotting tool used to plot graphs of functions or figures. It can also be used as an animation tool too. The plotted graphs when added with animations gives a more powerful visualization and helps the presenter to catch a larger number of audience.

How to convert animation to Jupyter Notebook?

What we can do is convert the animation into an HTML5 video and embed it in Jupyter Notebook. We will be using FFmpeg for conversion. If you don’t have it, you first need to follow the instruction to download FFmpeg and unzip it.

What is Matplotlib used for?

Matplotlib is one of the most popular plotting libraries for exploratory data analysis. Plotting a static graph should work well in most cases, but when you are running simulations or doing time-series data analysis, basic plots may not always be enough. You may want to show an animation that helps you understand how the state changes over time.


2 Answers

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.

like image 197
busybear Avatar answered Oct 13 '22 18:10

busybear


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.

like image 28
ImportanceOfBeingErnest Avatar answered Oct 13 '22 18:10

ImportanceOfBeingErnest