【发布时间】:2021-04-12 22:32:51
【问题描述】:
我正在学习 Python 的 matplotlib FuncAnimation 方法,但无法运行基本的贝叶斯共轭动画。当我运行它时,我的 Jupyter 笔记本中只得到一个空白输出。我也使用 Google Colab 得到相同的结果。如何让动画运行和显示?
注意,我可以运行官方动画教程脚本...只是下面代码的问题。
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gamma, poisson
np.random.seed(0)
fig, ax = plt.subplots()
line, = ax.plot([], [])
def animate(i):
if i == 0:
x = np.linspace(0, 30, 1000)
alpha = 2
beta = 1
y = gamma.pdf(x, a=alpha, scale=beta)
line.set_data(x, y)
return line,
else:
sample = poisson.rvs(4)
alpha += sample
beta += 1
y = gamma.pdf(x, a=alpha, scale=beta)
line.set_data(x, y)
return line,
%matplotlib notebook
%matplotlib notebook
anim = FuncAnimation(fig, animate, frames=2, blit=True)
plt.show()
【问题讨论】:
标签: python matplotlib jupyter-notebook matplotlib-animation