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?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
zazizoma
  • 437
  • 1
  • 7
  • 18
  • Here are two very basic examples for animations: http://stackoverflow.com/questions/42722691/python-matplotlib-update-scatter-plot-from-a-function – ImportanceOfBeingErnest Mar 13 '17 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. – zazizoma Mar 13 '17 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. – ImportanceOfBeingErnest Mar 13 '17 at 19:52

1 Answers1

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()
Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 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)`? – zazizoma Mar 13 '17 at 20:32
  • [`FuncAnimation` **is** a `TimedAnimation`](https://github.com/matplotlib/matplotlib/blob/f697e8cde48f54ddaef3e0172df25c8bc46bbed6/lib/matplotlib/animation.py#L1406). Yes, correct, `len(x)` must be `len(y)` - this just worked because both are the same in this example. – ImportanceOfBeingErnest Mar 13 '17 at 20:38
  • I really wonder how people can write *"Awesome, thank you"* below an answer, accept it, and finally **not** [upvote](http://stackoverflow.com/help/someone-answers) it. – ImportanceOfBeingErnest Mar 13 '17 at 21:09
  • 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? – zazizoma Mar 13 '17 at 23:29
  • 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? – ImportanceOfBeingErnest Mar 13 '17 at 23:49