<p>您可以有第二个<code>scatter</code>绘图,红色,具有更高的<code>zorder</code>,并更新其点:</p>
<pre><code>import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
plt.ion()
def update(time, red_plotted_points, points, N):
indexes = set([random.choice(range(N)) for i in range(int(N/2))]) # for instance!
new_points = [points[i] for i in indexes]
red_plotted_points.set_offsets(new_points)
def animate(N):
x = [random.random() for val in range(N)]
y = [random.random() for val in range(N)]
points = [[x[i], y[i]]for i in range(N)]
fig = plt.figure()
ax = fig.add_subplot(111)
plotted_points = ax.scatter(x, y, color='gray', zorder=1)
red_plotted_points = ax.scatter([], [], color='red', zorder=2) # starts empty
fargs = (red_plotted_points, points, N)
ani = FuncAnimation(fig, update, frames=range(100), fargs=fargs,
interval=200, repeat=True)
ani._start()
fig.show()
return fig, ani
if __name__ == '__main__':
fig, ani = animate(100)
</code></pre>
<p>(python 2.7.14,matplotlib 2.1.1)</p>
<blockquote>
<p>Edit: Updated to also run on Python 3.6.3, matpotlib 2.1.0</p>
</blockquote>
<p>我不知道为什么,但似乎如果不保留对<code>FuncAnimation</code>的引用,它在python3.6上就不起作用。感谢乔尔(评论)的注意。你知道吗</p>