首页 > 解决方案 > 关于动画中改变scatter的marker的问题

问题描述

在点移动动画期间,我在散布中改变“三角形标记”的方向时遇到问题。所以我有 100 个点在该区域上随机移动,并且在每次迭代中它们都可以改变移动方向(每个点与另一个点分开/它们是独立的)。我从每个迭代中每个点的pickle位置读取它们的方向,我希望绘制它。每 10 个移动点会被另一代仍在随机移动的点改变。我的代码有点工作,但不像它应该的那样。每个点的方向都是在每一代开始时设置的,但在每次迭代后都不会改变。有人可以帮我改变它吗?;)

def update(i, data, agent, texts, NUMBER_OF_POINTS): 

    x_N, y_N = [], []
    x_S, y_S = [], []
    x_W, y_W = [], []
    x_E, y_E = [], []
    for point, coordinate in IT.islice(data, NUMBER_OF_POINTS):
        if point in range(100):
            if coordinate[1] == Direction.N:
                texti = texts[point]
                x, y = coordinate[0].cpu().numpy()
                x_N.append(x)
                y_N.append(y)
                texti.set_position((x, y))
                agent_N.set_offsets(np.column_stack([x_N, y_N]))
                agent_N.set_paths([MarkerStyle("^").get_path().transformed(MarkerStyle("^").get_transform())])
            elif coordinate[1] == Direction.S:
                texti = texts[point]
                x, y = coordinate[0].cpu().numpy()
                x_S.append(x)
                y_S.append(y)
                texti.set_position((x, y))
                agent_S.set_offsets(np.column_stack([x_S, y_S]))
                agent_S.set_paths([MarkerStyle("v").get_path().transformed(MarkerStyle("v").get_transform())])
            elif coordinate[1] == Direction.W:
                texti = texts[point]
                x, y = coordinate[0].cpu().numpy()
                x_W.append(x)
                y_W.append(y)
                texti.set_position((x, y))
                agent_W.set_offsets(np.column_stack([x_W, y_W]))
                agent_W.set_paths([MarkerStyle("<").get_path().transformed(MarkerStyle("<").get_transform())])
             else:
                texti = texts[point]
                x, y = coordinate[0].cpu().numpy()
                x_E.append(x)
                y_E.append(y)
                texti.set_position((x, y))
                agent_E.set_offsets(np.column_stack([x_E, y_E]))
     return [agent_N, agent_S, agent_W, agent_E] + texts

if __name__ == "__main__":
    matplotlib.animation.Animation._blit_draw = _blit_draw
    num_frames = NUMBER_OF_GENERATIONS*SINGLE_LIFETIME
    fig, ax = plt.subplots()
    agent_N = ax.scatter([0] * NUMBER_OF_POINTS, [0] * NUMBER_OF_POINTS, c="lightblue", s=100)
    agent_S = ax.scatter([0] * NUMBER_OF_POINTS, [0] * NUMBER_OF_POINTS, c="lightblue", s=100)
    agent_E = ax.scatter([0] * NUMBER_OF_POINTS, [0] * NUMBER_OF_POINTS, c="lightblue", s=100)
    agent_W = ax.scatter([0] * NUMBER_OF_POINTS, [0] * NUMBER_OF_POINTS, c="lightblue", s=100)
    texts = []
    for i in range(NUMBER_OF_AGENTS):
        t = ax.text(0, 0, str(i), fontsize=10, animated=True)
        texts.append(t)
    path = "output.txt"
    data = get_data(path)
    ani = FuncAnimation(fig, update, range(1, num_frames + 1), init_func=init, blit=True, fargs=(data, agent_N, agent_S, agent_W, agent_E, mushroom, toadstool, texts, title, NUMBER_OF_POINTS), interval=1000, repeat=False,)
    plt.grid(True)
    plt.show()

标签: pythonmatplotlib

解决方案


我评论的重点是,当我们无法运行您的代码时,几乎不可能告诉您您的代码出了什么问题。

无论如何,这是一个简单的动画,其中标记会根据行进方向而变化,也许这会帮助您弄清楚一些事情。如果没有,那么考虑提供一个实际的 MVCE

np.random.seed(12345)
N_points = 20
N_frames_per_direction = 20

O_path = MarkerStyle('o').get_path().transformed(MarkerStyle('o').get_transform())
N_path = MarkerStyle('^').get_path().transformed(MarkerStyle('^').get_transform())
S_path = MarkerStyle('v').get_path().transformed(MarkerStyle('v').get_transform())
E_path = MarkerStyle('>').get_path().transformed(MarkerStyle('>').get_transform())
W_path = MarkerStyle('<').get_path().transformed(MarkerStyle('<').get_transform())

x_init,y_init = 20*np.random.random(size=(2,N_points)) # initial position
dxes = np.concatenate([np.random.normal(loc=1, scale=1, size=(N_points,N_frames_per_direction)), 
                       np.zeros(shape=(N_points, N_frames_per_direction)),
                       np.random.normal(loc=-1, scale=1, size=(N_points, N_frames_per_direction)),
                       np.zeros(shape=(N_points, N_frames_per_direction))], axis=1)
dyes = np.concatenate([np.zeros(shape=(N_points, N_frames_per_direction)),
                       np.random.normal(loc=-1, scale=1, size=(N_points, N_frames_per_direction)),
                       np.zeros(shape=(N_points, N_frames_per_direction)),
                       np.random.normal(loc=1, scale=1, size=(N_points,N_frames_per_direction)),], axis=1)



fig, ax = plt.subplots()
sc = ax.scatter([],[], marker='o')
ax.set_xlim(-10,50)
ax.set_ylim(-25,25)

def init():
    sc.set_offsets(np.c_[x_init,y_init])
    return sc,

def animate(i):
    x,y = sc.get_offsets().T
    x += dxes[:,i]
    y += dyes[:,i]
    sc.set_offsets(np.c_[x,y])
    sc.set_paths([E_path if dx>0 
                  else W_path if dx<0 
                  else N_path if dy>0 
                  else S_path if dy<0 
                  else O_path for dx,dy in zip(dxes[:,i], dyes[:,i])])
    return sc,

ani = animation.FuncAnimation(fig, animate, init_func=init, frames=4*N_frames_per_direction, blit=True)
plt.show()

在此处输入图像描述


推荐阅读