1

Hello and thanks in advance for any tips and advice. I'm trying to create an animated scatterplot that changes dot color based on model results that I'm reading into python. The model results would be much easier to interpret if I could display an updating time stamp along with the animation.

I tried to incorporate the answer for to this post: Matplotlib animating multiple lines and text, but I think the fact that I'm using a scatter dataset instead of lines complicates the way the data needs to be returned, and I'm not sure how to correct the problem.

This code alternately flashes the time step and the scatter animation, which makes for a distracting and useless visual.

 # load result
testModel.addResult(testDye,type='dye')
xs = testModel.grid['x']
ys = testModel.grid['y']
zs = testModel.grid['z']

# graphing
fig = plt.figure()
ax = fig.add_subplot(111)
gcf = plt.gcf()
scat = ax.scatter(xs,ys)
timetext = gcf.text(0.2, 0.2, '', size = 14)

def animate(i):
    print i
    actDye = testModel.getResult(type='dye',layer=1,tIndex=i)
    scat.set_array((actDye/1000)*100) #update colors of points 
    timetext.set_text(i)
    return scat,

def init():
    actDye = testModel.getResult(type='dye',layer=1,tIndex=0)
    scat.set_array((actDye/1000)*100) #update colors of points
    return scat,

ani = animation.FuncAnimation(fig,animate,np.arange(0,200),init_func=init, interval=500, blit=True)

plt.show()

I think that returning timetext along with scat, would fix the problem (like it did for the other poster), but I can't get the syntax right. Switching to this block of code gives me the error that the 'PathCollection' object is not iterable.

def animate(i):
    print i
    actDye = testModel.getResult(type='dye',layer=1,tIndex=i)
    scat.set_array((actDye/1000)*100) #update colors of points 
    timetext.set_text(i)
    return tuple(scat,) + (timetext,)

What should I be doing differently? Thanks!

Community
  • 1
  • 1
K. Smith
  • 11
  • 1

0 Answers0