0

I'm looking for a simple timed animation example for matplotlib. I've found several references to the subplot example in the matplotlib library but I need to see something much more basic on which to model my code.

I have 10 discrete values on the x axis, and a continuous value on the y axis (think histogram). The relationships between x and y change over 500 timesteps.

Here's a ridiculously truncated version of a dataset with just 5 categories and 5 timesteps:

x = list(range(0, 5)) y = [[2.00000000e-01, 2.00000000e-01, 2.75495888e-02, 1.40100625e-02, 2.00000000e-01], [1.40100625e-02, 3.85989938e-01, 6.20454173e-03, 1.74945474e-03, 2.00000000e-01], [1.74945474e-03, 3.98250545e-01, 1.24956950e-03, 2.30229281e-04, 2.00000000e-01], [2.30229281e-04, 3.99769771e-01, 2.26476892e-04, 3.05018276e-05, 2.00000000e-01], [3.05018276e-05, 3.99969498e-01, 3.82455658e-05, 4.04459287e-06, 2.00000000e-01]] 

How would one animate such a dataset in matplotlib?

3
  • Here are two very basic examples for animations: stackoverflow.com/questions/42722691/… Commented Mar 13, 2017 at 19:32
  • Thanks @IoBE, I was thinking I wanted a simple demonstration of TimedAnimation, but is your recommendation to redraw each figure through a for loop and not use animation at all? I don't need interaction. Commented Mar 13, 2017 at 19:43
  • In my answer to the linked question there are two solutions. I don't recomment anything, because I do not have any information about what you want to do. Commented Mar 13, 2017 at 19:52

1 Answer 1

1

Here is the example from this question's answer, using the data from above.

enter image description here

import matplotlib.pyplot as plt import matplotlib.animation import numpy as np x = list(range(0, 5)) y = [[2.00000000e-01, 2.00000000e-01, 2.75495888e-02, 1.40100625e-02, 2.00000000e-01], [1.40100625e-02, 3.85989938e-01, 6.20454173e-03, 1.74945474e-03, 2.00000000e-01], [1.74945474e-03, 3.98250545e-01, 1.24956950e-03, 2.30229281e-04, 2.00000000e-01], [2.30229281e-04, 3.99769771e-01, 2.26476892e-04, 3.05018276e-05, 2.00000000e-01], [3.05018276e-05, 3.99969498e-01, 3.82455658e-05, 4.04459287e-06, 2.00000000e-01]] fig, ax = plt.subplots() sc = ax.scatter(x,y[0]) plt.ylim(-0.1,0.5) def animate(i): sc.set_offsets(np.c_[x,y[i]]) ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(y), interval=300, repeat=True) plt.show() 
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, thank you, so I don't need TimedAnimation at all. So the set_offsets command generates the multiple plots, and the FuncAnimation controls the looping through i, timing and repeats. What if there were 100 time steps? Change frames = len(x) to frames=len(y)?
FuncAnimation is a TimedAnimation. Yes, correct, len(x) must be len(y) - this just worked because both are the same in this example.
Maybe because they have know idea what upvoting an answer means. Isn't it redundant if I've accepted it? Why would one answer get an upvote and another not?
Upvoting and accepting are two completely different things. Accepting means that the answer actually answers the question. The main reason to accept an answer is to show that the question is solved and thus does not appear in the list of unsolved problems. Upvoting an answer means that the answer has been useful. You should always upvote those answers that have helped you solve a problem or that you consider of good quality or worthwile reading, not only those to your own questions. As writing awesome somehow shows that this answer has helped you, so why not upvote it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.