Python matplotlib动画随机跳到初始位置
我正在修改来自的代码,但我发现在我的动画中,每个粒子似乎都跳回到它的初始位置,但不是同时跳回所有粒子?我不明白为什么会出现这种情况(我输入的数据是否正确?) 我有一个模拟测试粒子围绕中心质量运行的程序。程序以新行分隔的块输出数据。每个块由每个粒子的一条新线组成,每条线有3个数字,每个维度1个数字 下面是有问题的代码:Python matplotlib动画随机跳到初始位置,python,animation,matplotlib,Python,Animation,Matplotlib,我正在修改来自的代码,但我发现在我的动画中,每个粒子似乎都跳回到它的初始位置,但不是同时跳回所有粒子?我不明白为什么会出现这种情况(我输入的数据是否正确?) 我有一个模拟测试粒子围绕中心质量运行的程序。程序以新行分隔的块输出数据。每个块由每个粒子的一条新线组成,每条线有3个数字,每个维度1个数字 下面是有问题的代码: import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.mplot3d.axes3d as p
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个时间步,):
谢谢。
让我们考虑一个较小的例子,有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.reformate(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)
到
让我们考虑一个较小的例子,具有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.reformate(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)
到
我曾遇到一些问题,使数据进入正确的形状,但甚至没有想过考虑跳过数据的时间步长。这解决了我所有的问题,非常感谢!我曾遇到一些问题,使数据进入正确的形状,但甚至没有想过考虑跳过数据的时间步长。这解决了我所有的问题,非常感谢!