简体   繁体   English

额外窗口 jupyter 笔记本 matplotlib 动画

[英]extra window jupyter notebook matplotlib animation

I've found another thread on this site with the same problem that I have.我在这个网站上找到了另一个线程,它和我有同样的问题。 But the solutions of that thread do not help me for some reason.但是由于某种原因,该线程的解决方案对我没有帮助。 The problem is this, I have this code:问题是这样的,我有这个代码:

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

now, just as in the previous thread, there are 2 plots created but even with the plt.close() and putting HTML(anim.to_jshtml()) in a different cell, and using %%capture on the other cell, I still get 2 plots after executing the HTML(anim.to_jshtml()) code.现在,就像在上一个线程中一样,创建了 2 个图,但即使使用plt.close()并将HTML(anim.to_jshtml())放在不同的单元格中,并在另一个单元格上使用%%capture ,我仍然执行HTML(anim.to_jshtml())代码后得到 2 个图。 What am I doing wrong?我究竟做错了什么?

I found out that the extra window disappears when I remove the init function (of course also from FuncAnimation) and thus just have the animate function.我发现当我删除 init 函数(当然也从 FuncAnimation 中)时,额外的窗口消失了,因此只有 animate 函数。 (This is because of the animate function not having to use the init function, and thus also works when using ax instead of plt in the init function) (这是因为 animate 函数不必使用 init 函数,因此在 init 函数中使用 ax 而不是 plt 时也有效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM