【问题标题】:Matplotlib animate showing up empty plot even while using a basic tutorials即使在使用基本教程时,Matplotlib 动画也会显示空白图
【发布时间】:2020-12-05 22:43:06
【问题描述】:

我使用了来自this StackOverflow question.的确切代码

当我运行这段代码时,我看到的就是这些:

我希望我能提供更多详细信息,但我很困惑为什么即使是这个简单的示例也没有正确显示。

【问题讨论】:

  • 你在用 jupyter notebook 吗?
  • @ombk - 是的,我是
  • %matplotlib notebook 将此添加到您的代码中,稍后谢谢
  • @ombk 成功了
  • 很高兴它成功了^_^

标签: python matplotlib jupyter matplotlib-animation


【解决方案1】:

我看到你使用 Jupyter Notebook,所以你应该运行这个命令来显示 matplotlib 结果:

%matplotlib inline

您还需要使用来自IPython.displayHTML。然后将您的动画转换为 html 视频传递给这个 HTML 函数:

HTML(anim.to_html5_video())

下面有一个完整的例子。我正在使用您在问题中引用的the code 并对其进行了一些更改:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML


dt = 0.01
tfinal = 5.0
x0 = 0


sqrtdt = np.sqrt(dt)
n = int(tfinal/dt)
xtraj = np.zeros(n+1, float)
trange = np.linspace(start=0,stop=tfinal ,num=n+1) 
xtraj[0] = x0

for i in range(n):
    xtraj[i+1] = xtraj[i] + np.random.normal() 

x = trange
y = xtraj

# animation line plot example

fig = plt.figure(4)
ax = plt.axes(xlim=(-5, 5), ylim=(0, 5))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    line.set_data(x[:i], y[:i])
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(x)+1,interval=200, blit=False)


HTML(anim.to_html5_video())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    相关资源
    最近更新 更多