Home > Blockchain >  How to I make this program stop running?
How to I make this program stop running?

Time:06-25

This is the code, if you are using spyder you have to type %matplotlib in the console to get it working. The animation stops running but still loads of numbers appear in the console. How do I just call an end to the code?

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) 

CodePudding user response:

If you want to stop the animation, then in the function set, for example, it will stop at iteration number 15:

def animate(i):
   if i == 15:
      anim.event_source.stop()
  • Related