0

I have a dataset that is of shape (n, x, 2). The length of x is fixed, such as 5. So for each n, I have an array of size (x, 2). For each n, I want to plot the first column of (x, 2) against x as an animation. I've tried doing this:

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()

This ends up giving something strange, and the animation also "flickers." Is there a standard way to plot this time of thing, such as via either matplotlib.animation or plotly express?

Update: Assume data is an (n, x, 2) array, such as:

data = np.random.randint(1, 100, size=((200, 5, 2))

Now, call spatial_animation(data) to get the plot: this doesn't really give a plot.

Timus
  • 10,974
  • 5
  • 14
  • 28
user6496380
  • 43
  • 1
  • 7

0 Answers0