0

So lets say I have a dictionary as follows:

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

So the way I would do a single plot of all 'a','b','c' lines would be (assuming figure has already been declared, etc.):

#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()

So how can I turn this into an animated 3D graph? I have already tried plt.ion() and plt.pause() but the animation is painfully slow.

explodingfilms101
  • 480
  • 2
  • 11
  • 23
  • Take a look at [this post](https://stackoverflow.com/questions/38118598/3d-animation-using-matplotlib) – JohanC Jan 05 '20 at 16:00
  • How do you interpret 'a', 'b' and 'c' as lines? What are x,y,z? What are the starting points? What is the variable `locations`? Can you create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – JohanC Jan 05 '20 at 22:24
  • How do you interpret 'a', 'b' and 'c' as lines? What are x,y,z? What are the starting points? What is the variable `locations`? Can you create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – JohanC Jan 05 '20 at 22:29
  • @JohanC I answered my question with a general solution – explodingfilms101 Jan 07 '20 at 01:47

1 Answers1

0

Here is the following general implementation that I used and it works pretty well (involves dictionaries):

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)

explodingfilms101
  • 480
  • 2
  • 11
  • 23