Davoud Taghawi-Nejad
Davoud Taghawi-Nejad

Reputation: 16796

Animated graphs in ipython notebook

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()

Upvotes: 18

Views: 12856

Answers (5)

P-Gn
P-Gn

Reputation: 24591

matplotlib has an animation module to do just that. However, examples provided on the site will not run as is in a notebook; you need to make a few tweaks to make it work.

Here is the example of the page below modified to work in a notebook (modifications in bold).


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import rc
from IPython.display import HTML

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro', animated=True)

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return ln,

def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)
rc('animation', html='html5')
ani
# plt.show() # not needed anymore

Note that the animation in the notebook is made via a movie and that you need to have ffmpeg installed and matplotlib configured to use it.

Upvotes: 0

Mateo Sanchez
Mateo Sanchez

Reputation: 1654

IPython widgets let you manipulate Python objects in the kernel with GUI objects in the Notebook. You might also like Sage hosted IPython Notebooks. One problem you might have with sharing widgets or interactivity in Notebooks is that if someone else doesn't have IPython, they can't run your work. To solve that, you can use Domino to share Notebooks with widgets that others can run.

Below are three examples of widgets you can build in a Notebook using pandas to filter data, fractals, and a slider for a 3D plot. Learn more and see the code and Notebooks here.




enter image description here




enter image description here




enter image description here




If you want to live-stream data or set up a simulation to run as a loop, you can also stream data into plots in a Notebook. Disclaimer: I work for Plotly.

Upvotes: 6

Luciano
Luciano

Reputation: 2418

If you use IPython notebook, v2.0 and above support interactive widgets. You can find a good example notebook here (n.b. you need to download and run from your own machine to see the sliders).

It essentially boils down to importing interact, and then passing it a function, along with ranges for the paramters. e.g., from the second link:

In [8]:
def pltsin(f, a):
    plot(x,a*sin(2*pi*x*f))
    ylim(-10,10)
In [9]:
interact(pltsin, f=(1,10,0.1), a=(1,10,1));

This will produce a plot with two sliders, for f and a.

Upvotes: 5

Aron Ahmadia
Aron Ahmadia

Reputation: 2405

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()

Upvotes: 7

goger
goger

Reputation: 593

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()

Upvotes: 9

Related Questions