Python matplotlib动画随机跳转到初始位置

时间:2016-04-21 13:50:30

标签: python animation matplotlib

我正在调整来自this matplotlib example的代码,但是我发现在我的动画中,每个粒子似乎都会跳回到它的初始位置,但不是所有粒子都在同一时间?我无法弄清楚为什么会出现这种情况(我正确输入数据吗?)。

我有一个程序模拟围绕中心质量的测试粒子。程序以新行分隔的块输出数据。每个块由每个粒子的新行组成,每行有3个数字,每个维度1个。

以下是相关代码:

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

以下是输出文件格式的示例(对于21个粒子和2个时间步,link to full file with all 100 timesteps):

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

让我们考虑一个2个时间步长和3个粒子的小例子:

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]])

如果我们使用data.reshape(3, -1, 3).swapaxes(1, 2),我们就会获得

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]]])

请注意,第一个粒子的位置变为[0,1,2]和[3,4,5]。 但我们希望第一个粒子的位置为[0,1,2]和[9,10,11]。

所以改为使用

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]]])

因此,改变

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

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