I have the following code that generates an animated plot:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x = []
y = []
t = 0
fig, ax = plt.subplots()
line, = ax.plot([0,0])
ax.set_ylim((-2,2))
ax.set_xlim((0,10))
def animate(i):
global x,y,t
line.set_ydata(y)
line.set_xdata(x)
x.append(t)
y.append(np.sin(t))
t += 0.05
return line,
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, interval=25, blit=True)
plt.show()
How can I make it so that the plot is scaled to fit the data? I would like the axes and scale to automatically adjust.