Вы должны использовать FuncAnimation . Это будет реализация для вашего примера -
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
fig=plt.figure()
ax1=fig.add_subplot(1,1,1)
x=list(range(20))
y=[n*2 for n in x]
def animate(i):
ax1.clear()
ax1.scatter(x[:i],y[:i])
plt.autoscale(enable=True, axis='both')
ani = FuncAnimation(fig, animate, interval=100)
plt.show()
Обновление
Для работы с данными, которые не могут быть предварительно вычислены в массиве, как показано выше, вы можете использовать аргумент frames FuncAnimation как показано ниже -
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
fig=plt.figure()
ax1=fig.add_subplot(1,1,1)
x=[]
y=[]
def frames():
for i in range(20):
yield i, i*2
def animate(args):
ax1.clear()
x.append(args[0])
y.append(args[1])
ax1.scatter(x, y)
plt.autoscale(enable=True, axis='both')
ani = FuncAnimation(fig, animate, frames=frames, interval=100)
plt.show()