python - 关于在动画期间更改分散标记的问题

标签 python matplotlib

在点移动动画期间改变分散中“三角形标记”的方向时遇到问题。 所以我有 100 个点,它们在该区域上随机移动,并且在每次迭代中它们都可以改变移动方向(每个点与另一个点分开/它们是独立的)。我从每次迭代中每个点的泡菜位置及其方向读取,我希望将其绘制出来。每 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()

最佳答案

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

无论如何,这里有一个简单的动画,其中标记根据行进方向而变化,也许这会帮助您弄清楚一些事情。如果没有,请考虑提供实际的 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()

enter image description here

关于python - 关于在动画期间更改分散标记的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089375/

相关文章:

python - 在空格和换行符上拆分 txt 文件中的数字列表,Python

Python - 将特定键的 json 数据转换为 csv 格式

python - 解析 Scrapy 中的相邻项

python - matplotlib 中用于 3D 曲面图的自定义颜色图

x Axis 上的 Python 条形字符串

python - 4 个季度轮类的连续 TRUE 数

python - 这种python如何索引: 'print(X_train[y_train == 0][0])' work in python?

python - 如何使用 Matplotlib 在对数刻度上显示次要刻度标签

python - matplotlib分组数据框散点图中的颜色错误

python - 如何更改 matplotlib colorbar 标签的字体属性?