0

I just try to understand how the animation with matplotlib works. And for this I looked up examples and did a copy paste just to let them run and to understand it.

Unfortunately, even the examples from the matplotlib site only show a empty graph (e.g. https://matplotlib.org/examples/animation/animate_decay.html).

Axes, grid, background - everthing is there, just no graph or animation.

I used e.g. this code from the site https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ :

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation


# First set up the figure, the axis, and the plot element we want to animate
fig5 = plt.figure(5, figsize=(15,15))
ax5 = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, =ax5.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    print(x,y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig5, animate, init_func=init,
                           frames=200, interval=200, blit=True)
plt.show()

This produces that - But why? empty graph

edit: I don't know what I am doing wrong but none of the solution or code example given here Animation in iPython notebook works.

What works is this:

 from matplotlib import rc, animation
 rc('animation', html='html5')

and then using

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

This produces a short 9 sec long video that runs and runs and repeats itself until I press the stop button.

Andreas K.
  • 389
  • 4
  • 17
  • 3
    The problem is not with the animation, but with jupyter notebook. See this question for a solution: https://stackoverflow.com/questions/43445103/inline-animations-in-jupyter – Diziet Asahi Jan 15 '18 at 08:59
  • OK. Thanks. I tried this: HTML(anim.to_html5_video()) – Andreas K. Jan 15 '18 at 09:32
  • [contd] But again I get an empty graph plus the error message that "JavaScript output is disabled in JupyterLab" I tried it in Spyder and pressed the run buttom: same result. – Andreas K. Jan 15 '18 at 09:41
  • In the code from the question you would be missing the `%matplotlib notebook` statement (make sure to restart the kernel when trying that). Else you may use any of the solutions reportedly working from [Inline animations in Jupyter](https://stackoverflow.com/questions/43445103/inline-animations-in-jupyter). From the edit of the question it is not clear what the problem is. It seems to run fine, doesn't it? – ImportanceOfBeingErnest Jan 15 '18 at 11:28

0 Answers0