【问题标题】:Matplotlib FuncAnimation: How to gray out previous trajectory of in 3D pixel tracking?Matplotlib FuncAnimation:如何使 3D 像素跟踪中的先前轨迹变灰?
【发布时间】:2020-12-06 23:21:42
【问题描述】:

我有 3 个列表,分别包含 x 坐标、y 坐标和 z 坐标。我正在尝试跟踪 3D 空间中的位置。我使用下面的代码:

fig = plt.figure()
ax = p3.Axes3D(fig)
def update(num, data, line):
    
    line.set_data(data[:2, :num])
    line.set_3d_properties(data[2, :num])

N = 4000
d3d=np.array([xdata,ydata,zdata])

line, = ax.plot(d3d[0, 0:1], d3d[1, 0:1], d3d[2, 0:1], color='blue')
ax.set_xlim3d([2.0, -2.0])
ax.set_xlabel('X')

ax.set_ylim3d([2.0, -2.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 4.0])
ax.set_zlabel('Z')
ani = animation.FuncAnimation(fig, update, N, fargs=(d3d, line), interval=10000/N, blit=False)
plt.show()

有了这个,我可以成功地看到蓝色的轨迹。但是,我想以蓝色查看更新后的轨迹,并希望将之前的轨迹变灰:

我尝试在更新功能中使用下面的内容,所以上一行显示为灰色:

def update(num, data, line):
    
    line.set_data(data[:2, :num])
    line.set_3d_properties(data[2, :num])
    if line is not None:
        line.set_color('gray')

但这只会使整个轨迹变灰。任何帮助将不胜感激。

【问题讨论】:

  • 我认为您需要创建两条线:一条用于旧的灰线,一条用于新的蓝线,然后随着时间的推移不断更新它们。单个line3D 实例在matplotlib 中不能有多种颜色

标签: python python-3.x matplotlib matplotlib-animation


【解决方案1】:

我们可以跟踪绘制的线条并改变它们的颜色。

import matplotlib.pyplot as plt
import matplotlib.animation as anim
import numpy as np

fig = plt.figure()
ax = fig.gca(projection="3d")

#random data
np.random.seed(12345)
d3d = np.random.random((3, 12))

line_list = []
#number of line segments to retain in blue before greying them out
line_delay = 4    

def init():
    ax.clear()
    #you can omit the fixed scales, then they will be automatically updated
    ax.set_xlim3d(0, 1)
    ax.set_ylim3d(0, 1)
    ax.set_zlim3d(0, 1)        

def update(i):
    #initializing the plot, emptying the line list 
    if not i:
        init()
        line_list[:] = []
    
    #set line color to grey if delay number is exceeded
    if len(line_list)>=line_delay:
        line_list[-line_delay].set_color("grey")
    
    #plot new line segment
    newsegm, = ax.plot(*d3d[:, i:i+2], "blue") 
    line_list.append(newsegm)

ani = anim.FuncAnimation(fig, update, init_func=init, frames = np.arange(d3d.shape[1]), interval = 300, repeat=True)
plt.show()

这种方法的优点是我们可以轻松地调整它以更好地表示数据 - 例如,如果我们有很多数据,我们可以让它们淡化并移除所有不可见的线条:

import matplotlib.pyplot as plt
import matplotlib.animation as anim
import numpy as np

fig = plt.figure()
ax = fig.gca(projection="3d")

#random data
np.random.seed(12345)
d3d = np.random.random((3, 40))

#defines the number of disappearing line segments
max_length = 20

line_list = []

def init():
    ax.clear()
    ax.set_xlim3d(0, 1)
    ax.set_ylim3d(0, 1)
    ax.set_zlim3d(0, 1)

def update(i): 
    if not i:
        init()
        line_list[:] = []   
             
    else:
        #if not the first line segment, change color to grey, 
        line_list[-1].set_color("grey")
        #then reduce gradually the alpha value for all line segments
        diff2max = max(0, max_length-len(line_list))
        [x.set_alpha((j+diff2max)/max_length) for j, x in enumerate(line_list)]
    
    #delete line segments that we don't see anymore to declutter the space
    if len(line_list)>max_length:
        del_line = line_list.pop(0)
        del_line.remove()    
        
    #plot new segment and append it to the list
    newsegm, = ax.plot(*d3d[:, i:i+2], "blue") 
    line_list.append(newsegm)

ani = anim.FuncAnimation(fig, update, init_func=init, frames = np.arange(d3d.shape[1]), interval = 300, repeat=True)
plt.show()

示例输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2023-03-10
    • 2017-03-23
    相关资源
    最近更新 更多