我在散点图中有一堆重叠的点。我正在使用FuncAnimation
来创建动画。在连续的帧中,我想改变哪些出现在其他帧之前。
作为一个简单的MCVE,请考虑下面的代码,其中每个帧使一组不同的点为红色。然而,这些红点通常在很大程度上被其他点掩盖。我想让这些红点出现在前面。
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
def update(time, plotted_points, N):
#in here I would like to update the order that the points are plotted in. I would like the red points to come to the front.
color_change_indices = set(random.choices(range(N), k=1000))
colors = ['red' if n in color_change_indices else 'gray' for n in range(N)]
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)
plotted_points = ax.scatter(x, y, color='gray')
fargs = (plotted_points, N)
ani = FuncAnimation(fig, update, frames = range(100), fargs = fargs)
plt.show()
animate(10000)
我可以改变颜色。我可以移动他们的坐标。但是,到目前为止,我无法修改它们的相对深度。
到目前为止,我最好的想法是,我应该删除绘制的点,然后重新绘制它们。但我对matplotlib没有深刻的理解,到目前为止,我删除它们的尝试都失败了。
答案 0 :(得分:1)
您可以获得第二个scatter
图,红色并且zorder
更高,并更新其积分:
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)
(python 2.7.14,matplotlib 2.1.1)
编辑:已更新,也可在Python 3.6.3,matpotlib 2.1.0上运行
我不确定原因,但似乎如果没有将引用保留到FuncAnimation
,它就不适用于Python 3.6。感谢Joel(评论)注意到。
答案 1 :(得分:0)
为了在前面加上红点,您需要对红点最后的数据进行排序。在下面的代码中,不是按颜色对点进行排序,而是在每次调用更新时,将数据随机抽取并用红色绘制最后1000个点。
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)