如何对数据字典进行动画处理(即如何对多个3D数据点进行动画处理)?



假设我有一本字典,如下所示:

dictionary = {'a': [1,2,3], 'b':[4,2,5], 'c':[5,9,1]}

因此,我将对所有"a","b","c"行进行单个绘图的方式是(假设数字已经声明等(:

#half-setup for animation
lines = []
mass = list(dictionary.keys()) #I know this is redundant but my 'mass' variable serves another purpose in my actual program
for i in range(len(mass)): #create a list of line objects with zero entries
a, = ax.plot([], [])
lines.append(a)
#single plot
for i in dictionary:
index = np.array(locations[i]) #convert to numpy
ax.plot(index[:,0],index[:,1],index[:,2])
plt.show()

那么我怎样才能把它变成一个动画的3D图形呢?我已经尝试过 plt.ion(( 和 plt.pause((,但动画非常慢。

这是我使用的以下一般实现,它运行良好(涉及字典(:

import matplotlib.animation as anim
#create regular 3D figure 'fig'
lines = []
for i in range(3): #create however many lines you want
a, = ax.plot([],[],[]) #create lines with no data
lines.append(a)
bodies = {i:[data] for i in lines} #where [data] is your x,y,z dataset that you have before hand and 'i' is your matplotlib 'line' object
def update(num):
for i in bodies: #update positions of each line
index = np.array(bodies[i])
i.set_data(index[:,0][:num],index[:,1][:num])
i.set_3d_properties(index[:,2][:num])
if __name__=='__main__':
totalSteps = 1000 #can change
ani = anim.FuncAnimation(fig, update, totalSteps, interval = 1)

最新更新