Python 使用matplotlib使用colorbar设置散点图动画
我花了大量时间试图制作散点图,其中标记的颜色由一个数字定义 以下是我的尝试,虽然效果不错,但实际上并没有按计划进行:Python 使用matplotlib使用colorbar设置散点图动画,python,matplotlib,animation,Python,Matplotlib,Animation,我花了大量时间试图制作散点图,其中标记的颜色由一个数字定义 以下是我的尝试,虽然效果不错,但实际上并没有按计划进行: 在每个动画步骤之后,应删除旧点。相反,新点只需添加到绘图上已有的点即可 颜色栏也应该在每一步根据值进行更新(就像时间文本一样) 然而,无论我做什么,都会产生一个空白图表。这真的是python在设置散布动画时所能做到的最好的方法吗 import matplotlib.pyplot as plt import matplotlib.animation as animation i
- 在每个动画步骤之后,应删除旧点。相反,新点只需添加到绘图上已有的点即可
- 颜色栏也应该在每一步根据值进行更新(就像时间文本一样)
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)
我不确定您是否在帖子中指出了这一点,但我无法让您的代码按原样运行。然而,我相信主要问题与您提到的第一点有关:“在每个动画步骤之后,旧的点应该被删除。”在绘制动画时,您确实需要明确这一点。当前,您的代码正在重复为相同的
轴创建散布。就像在动画之外执行此操作一样,这将导致多组数据相互绘制
我见过两种主要的方法:使用绘图的一些设置方法更新数据(seed或或清除轴
或图
以绘制新数据。我发现后者更容易/更普遍(如果更懒惰的话)。以下是您的示例的一种方法:
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
;否则,他们中的许多人将被抽签。这意味着您每次都必须重新添加轴
和格式。在其他情况下,使用ax.clear()
可以很好地保存add\u subplot
的步骤,我不确定您是否在帖子中指出了这一点,但我无法让您的代码按原样运行。然而,我相信主要问题与您提到的第一点有关:“在每个动画步骤之后,旧的点应该被删除。”在绘制动画时,您确实需要明确这一点。当前,您的代码正在重复为相同的轴创建散布。就像在动画之外执行此操作一样,这将导致多组数据相互绘制
我见过两种主要的方法:使用绘图的一些设置方法更新数据(seed或或清除轴
或图
以绘制新数据。我发现后者更容易/更普遍(如果更懒惰的话)。以下是您的示例的一种方法:
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
;否则,他们中的许多人将被抽签。这意味着您每次都必须重新添加轴
和格式。在其他情况下,使用ax.clear()
可以很好地保存add_subplot
的步骤,我喜欢这个解决方案。简洁易懂。谢谢如果您希望颜色条不改变,您可以通过ax.scatter(..,vmin=0,vmax=1)
强制其限制。我喜欢这个解决方案。简洁易懂。谢谢如果您希望颜色条不改变,可以通过ax.scatter(..,vmin=0,vmax=1)
强制其限制。