【问题标题】:How to animate a dictionary of data (ie how to animate multiple 3D data points)?如何为数据字典制作动画(即如何为多个 3D 数据点制作动画)?
【发布时间】:2020-04-23 08:30:55
【问题描述】:

假设我有一本字典如下:

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() 但动画速度非常慢。

【问题讨论】:

  • 看看this post
  • 你如何将'a'、'b'和'c'解释为行? x,y,z 是什么?起点是什么?变量locations 是什么?你能创建一个minimal reproducible example吗?
  • 你如何将'a'、'b'和'c'解释为行? x,y,z 是什么?起点是什么?变量locations 是什么?你能创建一个minimal reproducible example吗?
  • @JohanC 我用通用解决方案回答了我的问题

标签: python numpy matplotlib 3d


【解决方案1】:

这是我使用的以下通用实现,它工作得很好(涉及字典):

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多