[Python] I cannot animate the scatter plot in matplotlib. Every frame is the repeat of the...

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    I am writing a python script to animate data from a list of .xyz files using matplotlib. However, the animation is actually static because each frame of the animation is displaying the data from the first frame itself.

    The format of the .xyz files is as follows:

    0.0199995 0.02 0.0199999
    0.0397943 0.02 0.0199997
    0.059875 0.02 0.0199997
    0.0799158 0.0199993 0.02
    0.0999472 0.02 0.0199997
    0.119963 0.0199992 0.02
    0.139975 0.02 0.0199996
    0.159982 0.02 0.0199996
    0.179987 0.02 0.0199996
    0.19999 0.0199992 0.02
    0.219993 0.0199992 0.02
    0.239994 0.0199992 0.02


    First column represents X, second Y and third Z.

    for i in range(num_of_frames):
    # Reading the data from ./sph_sim_output/frame_%d.xyz, where %d is the frame number
    data_x, data_y, data_z = get_pt_data("./sph_sim_output/frame_000000.xyz", i)
    data.append((data_x, data_y, data_z)) #data_x, data_y, data_z are numpy vectors

    fig = plt.figure()
    ax = fig.add_subplot(projection="3d")
    x, y, z = get_pt_data("./sph_sim_output/frame_000000.xyz", 0)
    scatters = ax.plot(x,z,y) # I also tried this with ax.scatter

    def update_pts(frame, data,scatters):
    x = data[frame][0]
    y = data[frame][1]
    z = data[frame][2]
    scatters._offset3d = (x, z, y)
    return scatters

    anim = animation.FuncAnimation(fig, update_pts, num_of_frames, fargs=(data,scatters), interval=60, blit=False)
    output_filename = "waterdrop.mp4"
    anim.save(output_filename, fps=60, bitrate=5000, writer=video_writer, extra_args=video_extra_args)
    plt.close(fig)



    The data is organized as follows: the data is a list of tuples and each tuple contains three numpy arrays. Please refer to line 4 of the code above. I am not really sure what I am missing. I looked through 1 or 2 examples online, but it still doesn't help. I have plotted individual frames and they seem to be fine. I am pretty sure the problem might be in update_pts function, I just cannot figure it out.

    Continue reading...

Compartilhe esta Página