I'm trying to create an animated figure for showing multiple tens of thousands of data points, appearing over time, in a single figure. Using blit=True
to avoid redrawing everything every frame doesn't work without telling it to redraw everything every frame - I want to just draw the new data points.
Neither python matplotlib update scatter plot from a function or How to animate a scatter plot? answer the question because I want to add new artists, not just update existing ones with set_offsets
, which forces every point to be redrawn. The documentation says that it does support new artists:
func must return an iterable of all artists that were modified or created
I'm running matplotlib 3.1.1 on macOS 10.13 and RHEL7 - the MacOSX backend doesn't work at all for me with blitting, so manually selecting TkAgg on that system:
import matplotlib
matplotlib.use("TkAgg") # On macOS
import matplotlib.animation as animation
import matplotlib.pyplot as plt
def animate(n):
scatter = ax1.scatter([n], [n])
return [scatter]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlim(0, 20)
ax1.set_ylim(0, 20)
ani = animation.FuncAnimation(fig, animate, interval=100, blit=True)
plt.show()
But when running this, only the last plotted data point remains on the figure:
How can I use blitting to write new data points without drawing everything every frame?