在 matplotlib 动画中绘制不同颜色的点
Plot points with different colors in a matplotlib animation
我有这段代码:
fig,ax=subplots(figsize=(20,10))
#ax=plot(matriz[0],matriz[1],color='black',lw=0,marker='+',markersize=10)
#ax=plot(matriz[2],matriz[3],color='blue',lw=0,marker='o',markersize=10)
#show ()
def animate(i):
ax=plot((matt[i][0],matt[i][2]),(matt[i][1],matt[i][3]),lw=0,color='r-',marker='o',markersize=8)
return ax
anim=animation.FuncAnimation(fig,animate,frames=numlin, interval=1000,blit=True,repeat=0)
show()
我真的没有使用 matplotlib 的经验,但我的老板要求我(在每次迭代中)用不同的颜色绘制每个点(即点 1 为红色,点 2 为蓝色等等)。我想用不同的颜色绘制每个点,但它应该在下一次迭代中保持相同的颜色。
如何在 matplotlib 中执行此操作?
我想我明白你想做什么,是的,我认为这是可能的。首先,我设置了一些随机数据来模拟我认为你在 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()
函数,它只是 returns 一个空列表,否则你的第一个集合点数始终保持在轴上):
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
等 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-')
根据 OP 评论进行编辑
OP 希望将其扩展到更多点集,而不是简单地将它们全部作为参数附加到 plot 函数。这是一种方法(改变 animate()
函数 - 其余保持不变)
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
我有这段代码:
fig,ax=subplots(figsize=(20,10))
#ax=plot(matriz[0],matriz[1],color='black',lw=0,marker='+',markersize=10)
#ax=plot(matriz[2],matriz[3],color='blue',lw=0,marker='o',markersize=10)
#show ()
def animate(i):
ax=plot((matt[i][0],matt[i][2]),(matt[i][1],matt[i][3]),lw=0,color='r-',marker='o',markersize=8)
return ax
anim=animation.FuncAnimation(fig,animate,frames=numlin, interval=1000,blit=True,repeat=0)
show()
我真的没有使用 matplotlib 的经验,但我的老板要求我(在每次迭代中)用不同的颜色绘制每个点(即点 1 为红色,点 2 为蓝色等等)。我想用不同的颜色绘制每个点,但它应该在下一次迭代中保持相同的颜色。
如何在 matplotlib 中执行此操作?
我想我明白你想做什么,是的,我认为这是可能的。首先,我设置了一些随机数据来模拟我认为你在 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()
函数,它只是 returns 一个空列表,否则你的第一个集合点数始终保持在轴上):
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
等 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-')
根据 OP 评论进行编辑
OP 希望将其扩展到更多点集,而不是简单地将它们全部作为参数附加到 plot 函数。这是一种方法(改变 animate()
函数 - 其余保持不变)
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