擅长:python、mysql、java
<p>为了将红点放在前面,您需要对红点最后出现的数据进行排序。在下面的代码中,它不是按颜色对点进行排序,而是在每次调用update时对数据进行洗牌并用红色绘制最后1000个点。你知道吗</p>
<pre><code>import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
import numpy as np # np.random.shuffle
def update(time, plotted_points):
# Extract data and colors
data = plotted_points.get_offsets()
colors = plotted_points.get_facecolor()
# shuffle the data [N, 2] along the first axis
np.random.shuffle(data)
# Set shuffled data and reset colors
plotted_points.set_offsets(data)
plotted_points.set_color(colors)
return plotted_points
def animate(N):
x = [random.random() for val in range(N)]
y = [random.random() for val in range(N)]
fig = plt.figure()
ax = fig.add_subplot(111)
# Need to define colors at first.
colors = ['red' if idx < 1000 else 'gray' for idx in range(N)]
colors.reverse()
plotted_points = ax.scatter(x, y, color=colors)
fargs = (plotted_points,)
ani = FuncAnimation(fig, update, frames = range(100), fargs = fargs)
plt.show()
animate(10000)
</code></pre>