簡體   English   中英

額外窗口 jupyter 筆記本 matplotlib 動畫

[英]extra window jupyter notebook matplotlib animation

我在這個網站上找到了另一個線程,它和我有同樣的問題。 但是由於某種原因,該線程的解決方案對我沒有幫助。 問題是這樣的,我有這個代碼:

from matplotlib.animation import FuncAnimation
from IPython.display import HTML
a_lijst = np.arange(-1,2,0.1)
fig, ax = plt.subplots()
x = np.arange(-1,1,0.01)

def verandering(X,V,a):
    #u -> X'
    #v -> Y'
    u = V
    v = -X - (a*V**3 - V)
    return [u,v]

def euler(initx,inity,a,stapgrootte,periode):
    #initiële condities
    X = initx
    V = inity
    x = []
    y = []
    for i in range(0,periode):
        x.append(X)
        y.append(V)
        X += stapgrootte*verandering(x[-1],y[-1],a)[0]
        V += stapgrootte*verandering(x[-1],y[-1],a)[1]
    return x,y

def init():
    plt.xlabel('x')
    plt.ylabel('y')
    plt.xlim(-1,1)
    plt.ylim(-2,2)
    
# animation function. This is called sequentially
def animate(i):
    ax.clear()
    ax.set_ylim(-1,1)
    ax.set_xlim(-2,2)
    for coordinaat in coordinaten:
        x_i = coordinaat[0]
        y_i = coordinaat[1]
        x,y = euler(x_i,y_i,a_lijst[i],0.1,100)
        ax.plot(x,y)
    ax.set_title('a={}'.format(a_lijst[i]))
# call the animator. blit=True means only re-draw the parts that have changed.
anim = FuncAnimation(fig, animate, init_func=init,
                               frames=30, interval=100)
plt.close()
HTML(anim.to_jshtml())

現在,就像在上一個線程中一樣,創建了 2 個圖,但即使使用plt.close()並將HTML(anim.to_jshtml())放在不同的單元格中,並在另一個單元格上使用%%capture ,我仍然執行HTML(anim.to_jshtml())代碼后得到 2 個圖。 我究竟做錯了什么?

我發現當我刪除 init 函數(當然也從 FuncAnimation 中)時,額外的窗口消失了,因此只有 animate 函數。 (這是因為 animate 函數不必使用 init 函數,因此在 init 函數中使用 ax 而不是 plt 時也有效)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM