I've been trying to recreate this animation in matplotlib by roughly following the answer to this thread: matplotlib animation multiple datasets (It's a monte carlo approximation of PI).
I'd like to know how I can accumulate the frames in my plot so that each frame adds additional dots on top of the previously plotted ones, so that the dots get more and more dense as time progresses. Also, do I have to fundamentally rewrite my code, since the approximation of PI requires that I carry over the number of blue and red points counted of every iteration?
fig, ax = plt.subplots(figsize=(5,5))
plt.rcParams['animation.ffmpeg_path'] = r'C:\FFmpeg\bin\ffmpeg'
b_sc = ax.scatter([],[],s=1,c='b')
r_sc = ax.scatter([],[],s=1,c='r')
def init():
ax.set_ylim(0,1)
ax.set_xlim(0,1)
return b_sc,r_sc
def update(frame):
org = np.array([0,0])
r, b = (0,0)
np.random.seed(frame)
dots = np.random.rand(100,2)
blue = np.array([dot for dot in dots if np.linalg.norm(org-dot) <= 1])
red = np.array([dot for dot in dots if np.linalg.norm(org-dot) > 1])
dr, db = red.shape[0], blue.shape[0]
r += dr
b += db
pi = b/(frame*(100))*4
if db != 0:
b_sc.set_offsets(blue)
if dr != 0:
r_sc.set_offsets(red)
# ax.figure.canvas.draw()
return b_sc,r_sc
FFwriter = animation.FFMpegWriter(fps=144)
ani = FuncAnimation(fig, update, frames=range(1,100), interval=1,
init_func=init, blit=True)
plt.show()

globallist and plot this complete list in each frame.