【问题标题】:i have problem with animate function.it gives me an empty plot我有动画功能的问题。它给了我一个空的情节
【发布时间】:2021-01-03 03:34:42
【问题描述】:

我对 matplotlib 的 Animate 函数有一些问题。 这里的代码:它给了我一个空图。也许我需要下载其他一些模块?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation

x_data = []
y_data = []
fig, ax = plt.subplots()
ax.set_xlim(0, 105)
ax.set_ylim(0, 12)
line, = ax.plot(0, 0)


def animation_frame(i):
    x_data.append(i*10)
    y_data.append(i)

    line.set_x_data(x_data)
    line.set_y_data(y_data)
    return line,


ani = animation.FuncAnimantion(fig, func = animation_frame, frames = np.arange(0, 10, 0.01), interval = 10)
plt.show()
    

输出是:

  File "C:\Users\Utente\untitled0.py", line 28, in <module>
    ani = animation.FuncAnimantion (fig,func= animation_frame,frames=np.arange(0,10,0.01),interval=10)

*****AttributeError: module 'matplotlib.animation' has no attribute 'FuncAnimantion'*****

有一个空图!

有人可以帮助我吗?

【问题讨论】:

  • 如果这解决了您的问题,请考虑接受答案

标签: python matplotlib


【解决方案1】:

有一些错别字:

  • FuncAnimantion -> FuncAnimation
  • set_x_data -> set_xdata
  • set_y_data -> set_ydata

否则它应该工作:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x_data = []
y_data = []
fig, ax = plt.subplots()
ax.set_xlim(0, 105)
ax.set_ylim(0, 12)
line, = ax.plot(0, 0)


def animation_frame(i):
    x_data.append(i * 10)
    y_data.append(i)

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line,

ani = animation.FuncAnimation(fig, func=animation_frame, frames=np.arange(0, 10, 0.01), interval=10)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多