这是代码,如果您正在使用spyder,则必须在控制台中键入%matplotlib才能使其工作。动画停止运行,但仍有大量数字出现在控制台中。我该如何结束代码?
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
plt.style.use('dark_background')
fig = plt.figure()
ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50))
line, = ax.plot([], [], lw=2)
# initialization function
def init():
# creating an empty plot/frame
line.set_data([], [])
return line,
xdata, ydata = [], []
x = 0
y = 0
# animation function
def animate(i):
print(i)
t = i*0.1
y = 0
x = t
if x > 10 and y == 0:
i = 0
x = 10
y = t - 10
if y >10 and x == 10:
x = 10 - (t-20)
y = 10
if (y < 10 or y == 10) and x <= 0 and y!=0:
y = 10 - (t-30)
x = 0
if y<0:
plt.stop()
exit()
print(y)
xdata.append(x)
ydata.append(y)
line.set_data(xdata, ydata)
return line,
# setting a title for the plot
plt.title('Creating a moving point')
# hiding the axis details
plt.axis('off')
# call the animator
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=500, interval=20, blit=True, repeat=False)
如果你想停止动画,那么在函数集中,例如,它将在第15次迭代时停止:
def animate(i):
if i == 15:
anim.event_source.stop()