Search code examples
pythonjupytermatplotlib-animation

Animation shows only a static picture - jupyter notebook


I found an interesting animation of a particle system that I would like to implement on jupyter notebook. So I copied the code in the file collision.py and pasted it on my jupyter notebook. After executing the code I am getting only a static picture

enter image description here

I tried to include the magic command %matplotlib inline but it still does not work. Does someone know why?


Solution

  • When I run the code you showed, collision.mp4 will be saved locally and I can play it as an animation. If you want to run the animation in jupyter environment, you need to install the following code.

    # Jupyter lab
    from IPython.display import HTML
    %matplotlib inline
    
    (Omission)
    
    # plt.show()
    
    # jupyter lab 
    plt.close()
    HTML(anim.to_html5_video())
    

    This is just one example of what I do. Please try this SO answer as it is very helpful.