【问题标题】:extra window jupyter notebook matplotlib animation额外的窗口 jupyter notebook matplotlib 动画
【发布时间】:2020-10-30 15:30:32
【问题描述】:

我在这个网站上发现了另一个 thread,我遇到了同样的问题。但是由于某种原因,该线程的解决方案对我没有帮助。问题是这样的,我有这个代码:

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,我仍然得到 2 个图之后执行HTML(anim.to_jshtml()) 代码。我做错了什么?

【问题讨论】:

    标签: python matplotlib jupyter-notebook


    【解决方案1】:

    我发现当我删除 init 函数(当然也来自 FuncAnimation)时,额外的窗口消失了,因此只有 animate 函数。 (这是因为 animate 函数不必使用 init 函数,因此在 init 函数中使用 ax 而不是 plt 时也可以使用)

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2018-01-25
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多