第一次运行这个代码块,但不是随后运行的时候,我会得到一个错误。有人能帮我理解为什么吗?
以前没有关于此错误的SO文章(至少在搜索结果中)
我对编码还比较陌生。据我所知,这是我的代码中的序列错误。也就是说,第一次属性没有被加载(还没有加载),然后属性会被进一步加载到代码块中,使它在我再次运行代码时可用。问题是我不知道代码的哪一部分。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
%matplotlib notebook
def data_gen(t=0):
cnt = 0
while cnt < 150:
cnt += 1
t += 0.1
yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
def init():
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 10)
del xdata[:]
del ydata[:]
line.set_data(xdata, ydata)
return line,
fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []
def run(data):
# update the data
t, y = data
xdata.append(t)
ydata.append(y)
xmin, xmax = ax.get_xlim()
if t >= xmax:
ax.set_xlim(xmin, 2*xmax)
ax.figure.canvas.draw()
line.set_data(xdata, ydata)
return line,
ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=15,
repeat=False, init_func=init)
plt.show()
错误信息
> Traceback (most recent call last): File
> "/Users/alexfreeman/Documents/Dev/AnacondaInstall/anaconda/envs/py3-env/lib/python3.6/site-packages/matplotlib/cbook/__init__.py",
> line 387, in process
> proxy(*args, **kwargs) File "/Users/alexfreeman/Documents/Dev/AnacondaInstall/anaconda/envs/py3-env/lib/python3.6/site-packages/matplotlib/cbook/__init__.py",
> line 227, in __call__
> return mtd(*args, **kwargs) File "/Users/alexfreeman/Documents/Dev/AnacondaInstall/anaconda/envs/py3-env/lib/python3.6/site-packages/matplotlib/animation.py",
> line 1499, in _stop
> self.event_source.remove_callback(self._loop_delay) AttributeError: 'NoneType' object has no attribute 'remove_callback'