【问题标题】:How to make an animation? [closed]如何制作动画? [关闭]
【发布时间】:2021-10-18 23:24:07
【问题描述】:

帮助! 我正在尝试制作一个为微分方程的解设置动画的函数,但是它没有向我显示任何动画。有什么问题吗?

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from matplotlib.animation import FuncAnimation

def animation(res, cap, l, i, fem):
    neper_frec = float(res / l)
    nat_frec = float(1/(cap*1)
    initial_charge = float(cap*fem)
    interval = np.linspace(0, 10, 600)
    def ode(y, t):
        charge, current = y
        dydt = [current, -neper_frec*current - nat_frec*charge]
        return dydt
    y0 = [initial_charge, i]
    sol = odeint(EDO, y0, interval)

    fig, ax = plt.subplots()
    ax.set(xlim=(0, 10), ylim=(-initial_charge, initial_charge))
    line, = ax.plot([], [])

    y = sol[:, 0]
    def animate(i):
        line.set_data(interval[:i], y[:i])
        return line,
    anim = FuncAnimation(fig, animate, frames = len(interval) + 1, interval = 20, blit = True)
    plt.show()

#animacion(1.4, 1/600, 1.7, 0.5, 8)

【问题讨论】:

  • Why is “Can someone help me?” not an actual question?。请参阅How to ask a good question。始终提供完整的minimal reproducible example,其中包含代码、数据、错误、当前输出和预期输出,如formatted text。如果相关,只有绘图图像是可以的。
  • 调用函数def animation(res, cap, l, i, fem)的代码在哪里??如果这是你所有的代码,你只是定义一个函数,而不是调用它。
  • 如果我取消注释 #animacion(1.4, 1/600, 1.7, 0.5, 8) 并将拼写修正为 animation(),那么我会得到 File "tmp_anim.py", line 16, in animation sol = odeint(EDO, y0, interval) NameError: name 'EDO' is not defined

标签: python matplotlib animation ode subplot


【解决方案1】:
  1. 取消评论#animacion(1.4, 1/600, 1.7, 0.5, 8)
  2. 正确拼写为animation(1.4, 1/600, 1.7, 0.5, 8)
  3. 将EDO 的错字更正为ode:
sol = odeint(ode, y0, interval)

然后动画效果很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2021-05-16
    • 2018-09-08
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多