I think I see what you want to do and, yes, I think it is possible. First, I have set up some random data to simulate what I think you have in matt
from random import random as r
numlin=50
matt = []
for j in xrange(numlin):
matt.append([r()*20, r()*10,r()*20,r()*10])
Now, using your code as closely as possible, I think you want to do this (I've added an init()
function, which just returns an empty list, otherwise your first set of points stays on the axis throughout):
from matplotlib.pyplot import plot, show, subplots
import matplotlib.animation as animation
fig,ax=subplots(figsize=(20,10))
ax.set_xlim([0,20])
ax.set_ylim([0,10])
def animate(i):
animlist = plot(matt[i][0],matt[i][1],'r',matt[i][2],matt[i][3],'b',marker='o',markersize=8)
return animlist
def init():
return []
anim=animation.FuncAnimation(fig,animate,frames=numlin,interval=1000,init_func=init,blit=True,repeat=0)
show()
How it works
Passing in sets of (x0,y0,c0, x1,y1,c1, x2,y2,c2 ... )
into plot()
is valid where cx
is a valid matplotlib colour format. They go before any named **kwargs
like marker
etc. It's described in the docs here.
An arbitrary number of x, y, fmt groups can be specified, as in:
a.plot(x1, y1, 'g^', x2, y2, 'g-')
Edit in response to OP comment
OP wanted to make this extensible to more sets of points, without simply appending them all as arguments to the plot function. Here is one way (altering the animate()
function - the rest stays the same)
def animate(i):
#Make a tuple or list of (x0,y0,c0,x1,y1,c1,x2....)
newpoints = (matt[i][0],matt[i][1],'r',
matt[i][0],matt[i][3],'b',
matt[i][2],matt[i][3],'g',
matt[i][2],matt[i][1],'y')
# Use the * operator to expand the tuple / list
# of (x,y,c) triplets into arguments to pass to the
# plot function
animlist = plot(*newpoints,marker='o',markersize=8)
return animlist