Python matplotlib animation randomly jumping to initial position
Question: I'm adapting code from [this matplotlib
example](http://matplotlib.org/1.4.1/examples/animation/simple_3danim.html)
but am finding that within my animation each particle seems to jump back to
it's initial position, but not all at the same time? I can't figure out why
this would be the case (am I inputting the data correctly?).

I have a program simulating test particles orbiting a central mass. The
program outputs data in blocks separated by a new line. Each block consists of
a new line for each particle, and each line has 3 numbers, 1 for each
dimension.

Here's the code in question:

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    import csv
    
    #read in data
    with open('tmpfile', 'rb') as csvfile: 
        reader = csv.reader(csvfile, delimiter='\t')
        data = np.array([[float(field) for field in row]
                            for row in filter(lambda x: x != [], reader)])
    print(data.shape)
    data = data.reshape((21, -1, 3)).swapaxes(1,2)
    print(data.shape)
    
    
    def update_points(num, dataPoints, points) :
        for point, data in zip(points, dataPoints) :
            point.set_data(data[0:2, num-1:num])
            point.set_3d_properties(data[2,num-1:num])
        return points
    
    #prepare plot
    fig = plt.figure()
    ax = p3.Axes3D(fig)
    
    points = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], c='b', marker='o')[0] for dat in data]
    
    # Set the axes properties
    ax.view_init(90, 90)
    
    ax.set_xlim3d([-8.0, 8.0])
    ax.set_xlabel('X')
    
    ax.set_ylim3d([-8.0, 8.0])
    ax.set_ylabel('Y')
    
    ax.set_zlim3d([-1.0, 1.0])
    ax.set_zlabel('Z')
    
    #Create the Animation object
    ani = animation.FuncAnimation(fig, update_points, 101, fargs=(data, points), 
                                       interval=500, blit=False)
    
    plt.show()
    

And here's an example of the output file format (for 21 particles and 2 time
steps, [link to full file with all 100
timesteps](https://www.dropbox.com/s/buv2ocqjblq7zty/tmpfile?dl=0)):

    
    
    0   0   0
    1.954   -0.4259 0
    0.7562  -1.852  0
    2.308   1.917   0
    -1.032  -2.817  0
    2.001   2.235   0
    3.813   1.208   0
    -1.888  3.526   0
    2.298   -3.274  0
    2.556   3.077   0
    -4.664  1.802   0
    2.719   4.196   0
    -3.991  3.012   0
    -4.018  2.976   0
    4.398   -2.379  0
    3.924   -4.539  0
    -1.954  -5.673  0
    -2.751  5.332   0
    3.87    4.585   0
    5.725   -1.796  0
    5.369   -2.678  0
    
    0   0   0
    1.956   -0.419  0
    0.7627  -1.849  0
    2.304   1.922   0
    -1.026  -2.819  0
    1.996   2.239   0
    3.812   1.212   0
    -1.892  3.524   0
    2.302   -3.271  0
    2.553   3.08    0
    -4.666  1.798   0
    2.715   4.198   0
    -3.994  3.008   0
    -4.02   2.973   0
    4.4 -2.375  0
    3.927   -4.536  0
    -1.95   -5.674  0
    -2.755  5.33    0
    3.867   4.588   0
    5.726   -1.792  0
    5.371   -2.674  0
    

Thanks in advance.


Answer: Let's consider a smaller example with 2 timesteps and 3 particles:

    
    
    In [175]: data = np.arange(6*3).reshape(6,3); data
    
    Out[175]: 
    array([[ 0,  1,  2],
           [ 3,  4,  5],
           [ 6,  7,  8],
           [ 9, 10, 11],
           [12, 13, 14],
           [15, 16, 17]])
    

If we use `data.reshape(3, -1, 3).swapaxes(1, 2)` then we obtain

    
    
    In [176]: data.reshape(3, -1, 3).swapaxes(1, 2)
    Out[176]: 
    array([[[ 0,  3],
            [ 1,  4],
            [ 2,  5]],
    
           [[ 6,  9],
            [ 7, 10],
            [ 8, 11]],
    
           [[12, 15],
            [13, 16],
            [14, 17]]])
    

Notice that the first particle's positions become [0,1,2] and [3,4,5]. But we
want the first particle's positions to be [0,1,2] and [9,10,11].

So instead use

    
    
    In [177]: data.reshape((-1, 3, 3)).transpose([1, 2, 0])
    Out[177]: 
    array([[[ 0,  9],
            [ 1, 10],
            [ 2, 11]],
    
           [[ 3, 12],
            [ 4, 13],
            [ 5, 14]],
    
           [[ 6, 15],
            [ 7, 16],
            [ 8, 17]]])
    

* * *

Therefore, change

    
    
    data = data.reshape((21, -1, 3)).swapaxes(1,2)
    

to

    
    
    data = data.reshape((-1, 21, 3)).transpose([1, 2, 0])
    

