我想我明白你想做什么,是的,我认为这是可能的。首先,我设置了一些随机数据来模拟我认为你在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])
现在,尽可能接近您的代码,我认为您想要做到这一点(我添加了一个
init()
函数,它只返回一个空列表,否则您的第一组点将始终留在轴上):
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()
工作原理
通过将(x0,y0,c0, x1,y1,c1, x2,y2,c2 ... )
这样的集合传递给plot()
是有效的,其中cx
是有效的matplotlib颜色格式。它们位于任何命名的**kwargs
之前,如marker
等。在文档中有描述。
An arbitrary number of x, y, fmt groups can be specified, as in:
a.plot(x1, y1, 'g^', x2, y2, 'g-')
回应OP的评论进行编辑
OP希望将此功能扩展到更多的点集,而不仅仅是将它们全部附加为绘图函数的参数。以下是一种方法(修改animate()
函数 - 其余部分保持不变)
def animate(i):
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')
animlist = plot(*newpoints,marker='o',markersize=8)
return animlist
ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)
中,参数(按顺序)分别为 x 向量、y 向量、颜色、大小、透明度。尝试通过传递正确大小的range
作为颜色参数来进行实验。一旦您喜欢的散点图绘制完成,FuncAnimation
将对它们执行与任何绘图相同的操作。 - cphlewis