2012-07-23 122 views
18

是否有創建動畫圖形的方法。例如顯示具有不同參數的同一圖表。ipython筆記本中的動畫圖形

例如是SAGE筆記本電腦,一個可以寫:

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

回答

7

更新:2014年1月

傑克Vanderplas創造了可here matplotlib動畫一個基於JavaScript的包。使用它很簡單:

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

Example of Jake's JSAnimation package

一個更完整的說明和示例見his blog post歷史答案(見goger一次修正)

是,JavaScript的更新不正確把握圖像幀還,所以存在閃爍,但是你可以用這種技術做一些非常簡單:

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

你能否也許增加一個合理的x值?我嘗試了一個簡單範圍(100)和numpy.arange(0,2 * pi,0.01)的示例,但都沒有創建出好的輸出。此外,似乎你錯過了一些進口來運行這個代碼,因爲這是 – 2013-03-05 19:25:24

+0

這個評論是在幾年後:**以避免閃爍**設置'clear_output(True)'(IPython 2.0.0) – 2014-03-02 00:53:00

+0

Thanks @eldad!我應該再次更新:) – 2014-03-02 03:52:57

9

這有可怕的閃爍,但至少這創造了一個情節,爲我的動畫。它基於阿隆的,但阿隆的不工作。

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

如果使用IPython筆記本,v2.0及以上版本支持interactive widgets。你可以找到一個很好的示例筆記本here(n.b.你需要下載並從你自己的機器上運行以查看滑塊)。

它實質上歸結爲導入interact,然後傳遞一個函數,以及參數的範圍。例如,從第二鏈路:

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

這將產生一個情節有兩個滑塊,用於fa

+0

我試圖在iPython筆記本中創建不同類型的事情,這看起來像我迄今發現的最佳解決方案。有沒有辦法讓它自動生成動畫,以便用戶不需要操縱滑塊? – Addem 2016-02-21 04:16:50

+0

我不認爲這樣做是微不足道的。但是看看'events'的頁面:有一個使用回調來編程控制小部件的例子。所以從理論上講,你可以有一個按鈕,點擊時會運行一個'for'循環來調整滑塊/控件,然後在兩者之間休息。這將會有人「手動」滑動控制的效果。但這並不理想。 – Luciano 2016-03-07 11:07:18

5

IPython widgets讓你使用筆記本中的GUI對象來操作內核中的Python對象。你可能也喜歡Sage hosted IPython Notebooks。在筆記本中共享小部件或交互性可能會遇到的一個問題是,如果其他人沒有IPython,他們將無法運行您的工作。爲了解決這個問題,你可以使用Domino到share Notebooks以及其他人可以運行的小部件。

下面是三個可以在筆記本中使用熊貓過濾數據,分形和3D滑塊滑塊的小部件示例。瞭解更多信息並查看代碼和筆記本here




enter image description here




enter image description here




enter image description here




如果你想生活流數據或設置模擬爲一個循環運行,也可以stream data成一個筆記本地塊。免責聲明:我爲Plotly工作。

0

On @ goger對'可怕的閃爍'的評論,我發現調用clear_output(wait = True)解決了我的問題。該標誌告訴clear_output等待渲染,直到它具有新的渲染。

0

matplotlib有一個animation模塊來做到這一點。但是,網站上提供的示例不會像筆記本中那樣運行;你需要做一些調整才能使它工作。

下面是以下頁面的示例,修改爲在筆記本中使用(修改加粗)。


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 

注意的是,在筆記本電腦動畫是通過電影,你需要安裝的ffmpeg和matplotlib配置爲用它製成。