【发布时间】:2021-07-09 16:44:26
【问题描述】:
我有一个点列表,可以说是 (x,y) 对。我正在尝试对情节进行动画处理,以便动画的每一帧,一个新点以不同的颜色显示在情节上。具体到第0帧出现第0个点,第1帧出现第1个点,以此类推。我还想让这些点以一种新的颜色出现,特别是像随着点的进展通过调色板的线性进展,这样你就可以通过它们的颜色“跟随”这些点。这类似于,以及我现在的情况:How can i make points of a python plot appear over time?。链接中的第一个动画是正确的,除了点没有改变颜色。
我正在使用来自matplotlib.animation 的matplotlib、matplotlib.pyplot 和FuncAnimation
我已经拥有的:
def plot_points_over_time(list_of_points):
num_points = len(list_of_points)
fig = plt.figure()
x, y = zip(*list_of_points)
plt.xlim(min(x),max(x))
plt.ylim(min(y),max(y))
colors = [plt.cm.gist_rainbow(each) for each in np.linspace(0,1,num_points)]
graph, = plt.plot([],[],'o')
def animate(i):
graph.set_data(x[:i+1],y[:i+1])
return graph
ani = FuncAnimation(fig, animate, frames = num_points, repeat = False, interval = 60000/num_points)
plt.show()
我可以通过在animate 函数中包含线graph.set_color(colors[i]) 来更改每一帧上所有点的颜色,但不能单独更改每个点。
【问题讨论】:
标签: python matplotlib animation plot colors