我花费了大量时间尝试动画散点图,其中标记的颜色由数字定义。
下面是我的尝试,是哪种工作,但并非按计划进行:
但是,无论我做什么,都会生成一个空白图表。在动画散布方面,这真的是我能用python做的最好的事情吗?
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
time_steps = 50
N_nodes = 100
positions = []
solutions = []
for i in range(time_steps):
positions.append(np.random.rand(2, N_nodes))
solutions.append(np.random.random(N_nodes))
fig = plt.figure()
marker_size = 1
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(0, 1), ylim=(0, 1))
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes)
def init():
""" Initialize animation. """
scat = ax.scatter(positions[0][0], positions[0][1], s = marker_size, c = solutions[0], cmap = "RdBu_r", marker = ".", edgecolor = None)
fig.colorbar(scat)
time_text.set_text('Time step = %d' % 0)
return scat, time_text
def animate(i):
""" Perform animation step. """
scat = ax.scatter(positions[i][0], positions[i][1], s = marker_size, c = solutions[i], cmap = "RdBu_r", marker = ".", edgecolor = None)
time_text.set_text('Time step = %d' % i)
return scat, time_text
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.grid(b=None)
plt.show()
ani = animation.FuncAnimation(fig, animate, interval=100, blit=True, repeat=True, init_func=init)
ani.save('animation.gif', writer='imagemagick', fps = 8)
答案 0 :(得分:1)
我不确定您是否从帖子中指出了这一点,但是我无法按原样运行您的代码。但是,我认为主要问题与您提到的第一点有关:“ 在每个动画步骤之后,都应删除旧的点。”。在绘制动画时,您确实需要对此进行明确说明。当前,您的代码正在为同一scatter
重复创建Axes
。就像您要在动画之外执行此操作一样,这将导致多组数据相互绘制。
我已经看到人们执行此操作的两种主要方式:使用绘图的某些set_...
方法来更新数据(见here for scatter plots或here in general)每次迭代或清除Axes
或Figure
以便绘制新数据。我发现后者更容易/更通用(如果比较懒惰)。这是您的示例这样做的一种方法:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
time_steps = 50
N_nodes = 100
positions = []
solutions = []
for i in range(time_steps):
positions.append(np.random.rand(2, N_nodes))
solutions.append(np.random.random(N_nodes))
fig, ax = plt.subplots()
marker_size = 5 #upped this to make points more visible
def animate(i):
""" Perform animation step. """
#important - the figure is cleared and new axes are added
fig.clear()
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(0, 1), ylim=(0, 1))
#the new axes must be re-formatted
ax.set_xlim(0,1)
ax.set_ylim(0,1)
# and the elements for this frame are added
ax.text(0.02, 0.95, 'Time step = %d' % i, transform=ax.transAxes)
s = ax.scatter(positions[i][0], positions[i][1], s = marker_size, c = solutions[i], cmap = "RdBu_r", marker = ".", edgecolor = None)
fig.colorbar(s)
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.grid(b=None)
ani = animation.FuncAnimation(fig, animate, interval=100, frames=range(time_steps))
ani.save('animation.gif', writer='pillow')
产生以下GIF:
我使用fig.clear()
清除每帧colorbar
;否则,将吸引许多人。这意味着您每次都必须重新添加Axes
和格式。在其他情况下,可以使用ax.clear()
并节省add_subplot
的步骤。