我有一个形状为 (n, x, 2) 的数据集。x 的长度是固定的,比如 5。所以对于每个 n,我都有一个大小为 (x, 2) 的数组。对于每个 n,我想将 (x, 2) 的第一列与 x 绘制为动画。我试过这样做:
def spatial_animation(data):
"""
Args:
data: [n, n_x, 2]
"""
from matplotlib.animation import FuncAnimation
x = data.shape[1]
x = np.linspace(0, 1, x) # fixed array of size x
y = np.array([y[:, 0] for y in data])
fig = plt.figure()
graph, = plt.plot([], [])
def animate(i):
graph.set_data(x[:i+1], y[:i+1])
return graph
ani = FuncAnimation(fig, animate, frames=30, interval=200)
plt.show()
这最终给出了一些奇怪的东西,并且动画也“闪烁”。是否有一种标准的方式来绘制这个时间,例如通过 matplotlib.animation 或 plotly express?
更新:假设数据是一个 (n, x, 2) 数组,例如:
data = np.random.randint(1, 100, size=((200, 5, 2))
现在,打电话spatial_animation(data)
来获取情节:这并没有真正给出情节。