Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated graphs in ipython notebook

Tags:

plot

ipython

Is there a way of creating animated graphs. For example showing the same graph, with different parameters.

For example is SAGE notebook, one can write:

a = animate([circle((i,i), 1-1/(i+1), hue=i/10) for i in srange(0,2,0.2)], 
            xmin=0,ymin=0,xmax=2,ymax=2,figsize=[2,2])
a.show()
like image 812
Davoud Taghawi-Nejad Avatar asked Jul 23 '12 11:07

Davoud Taghawi-Nejad


People also ask

Can you animate in Jupyter notebook?

Animation features are built into the Python matplotlib library and available through Jupyter notebooks. Unfortunately, however, the documentation is not particularly robust. This short notebook provides simple demonstrations of using animiation for the visualization of simulation results.

Can matplotlib create animated graphs?

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.


2 Answers

This has horrible flickering, but at least this creates a plot that animates for me. It is based on Aron's, but Aron's does not work as-is.

import time, sys
from IPython.core.display import clear_output
f, ax = plt.subplots()

n = 30
x = array([i/10.0 for i in range(n)])
y = array([sin(i) for i in x])
for i in range(5,n):
  ax.plot(x[:i],y[:i])
  time.sleep(0.1)
  clear_output()
  display(f)
  ax.cla() # turn this off if you'd like to "build up" plots
plt.close()
like image 85
goger Avatar answered Sep 20 '22 09:09

goger


Update: January 2014

Jake Vanderplas has created a Javascript-based package for matplotlib animations available here. Using it is as simple as:

 # https://github.com/jakevdp/JSAnimation
 from JSAnimation import examples
 examples.basic_animation()

Example of Jake's JSAnimation package

See his blog post for a more complete description and examples. Historical answer (see goger for a correction)

Yes, the Javascript update does not correctly hold the image frame yet, so there is flicker, but you can do something quite simple using this technique:

import time, sys
from IPython.display import clear_output
f, ax = plt.subplots()

for i in range(10):
  y = i/10*sin(x) 
  ax.plot(x,y)
  time.sleep(0.5)
  clear_output()
  display(f)
  ax.cla() # turn this off if you'd like to "build up" plots
plt.close()
like image 28
Aron Ahmadia Avatar answered Sep 19 '22 09:09

Aron Ahmadia