Python 从输出txt matplotlib制作电影

Python 从输出txt matplotlib制作电影,python,animation,matplotlib,movie,Python,Animation,Matplotlib,Movie,我正在尝试使用matplotlib从包含n体问题解决方案的txt输出文件制作电影。我需要做的基本事情是在输出文件中选择具有一定时差的行(在近距离接触中,时间步长变小以保持精度,因此我获得更多的输出行),并使用这些行制作电影。我使用的输出文件的每一行都包含不同粒子的时间和位置 在网上,我找到了许多使用matplotlib制作动画的教程(例如,制作正弦波动画),但似乎找不到任何教程可以帮助我实现使用txt文件制作电影的目标 下面是我正在使用的输出文件中几行的示例: 0.0005-2-11-13 0.

我正在尝试使用matplotlib从包含n体问题解决方案的txt输出文件制作电影。我需要做的基本事情是在输出文件中选择具有一定时差的行(在近距离接触中,时间步长变小以保持精度,因此我获得更多的输出行),并使用这些行制作电影。我使用的输出文件的每一行都包含不同粒子的时间和位置

在网上,我找到了许多使用matplotlib制作动画的教程(例如,制作正弦波动画),但似乎找不到任何教程可以帮助我实现使用txt文件制作电影的目标

下面是我正在使用的输出文件中几行的示例:

0.0005-2-11-13
0.1505-1.99288-0.998912 0.99496-0.997876 0.998912 2.99501
0.3005-1.97152-0.995647 0.97983-0.991519 0.995647 2.98006
0.4505-1.93558-0.99016 0.954369-0.980896 0.99016 2.95504
0.6005-1.88448-0.982387 0.918153-0.965948 0.982391 2.91976
0.7505-1.81732-0.972201 0.870528-0.946598 0.972217 2.87393
0.9005-1.73279-0.959441 0.810535-0.922745 0.959494 2.81716
1.0505-1.62897-0.943865 0.736769-0.894279 0.944017 2.74895
1.2005-1.50304-0.925119 0.647125-0.861093 0.925512 2.66865
1.3505-1.35056-0.902646 0.538279-0.82313 0.903612 2.57541
1.5005-1.16395-0.875477 0.404466-0.780528 0.877819 2.46818
1.6505-0.92788-0.84154 0.233839-0.734119 0.847441 2.34559
1.7829-0.644524-0.800616 0.0259867-0.693428 0.816054 2.2232
1.8129-0.560299-0.78792-0.0367125-0.685743 0.808253 2.19346
1.8429-0.459578-0.771672-0.112441-0.680428-0.800171-2.16294
1.85368-0.416242-0.763979-0.145323-0.679882 0.797195 2.15178
1.85655-0.403697-0.761621-0.154881-0.679979 0.796398 2.14879
1.85919-0.391633-0.75928-0.16409-0.680198 0.795662 2.14604
1.86161-0.380057-0.756956-0.172945-0.680536 0.794983 2.1435
1.86383-0.368974-0.754647-0.181438-0.680989 0.794361 2.14118
1.86584-0.35839-0.75235-0.189563-0.681555 0.793793 2.13906
1.86767-0.348313-0.750063-0.197316-0.682229 0.793277 2.13713
1.86932-0.338746-0.747786-0.204689-0.683008 0.79281 2.13539
1.8708-0.329696-0.745515-0.211678-0.683887 0.792391 2.13383
1.87212-0.321167-0.743249-0.218277-0.684864 0.792017 2.13244
1.87329-0.313163-0.740986-0.224481-0.685933 0.791685 2.1312
1.87432-0.305688-0.738724-0.230285-0.68709 0.791393 2.13012
1.87521-0.298747-0.73646-0.235685-0.688332 0.791138 2.12917
1.87599-0.292342-0.734192-0.240677-0.689654 0.790917 2.12835
1.87665-0.286477-0.731918-0.245255-0.691052 0.790728 2.12764
1.87721-0.281154-0.729636-0.249417-0.69252 0.790568 2.12705
1.87768-0.276376-0.727345-0.25316-0.694055 0.790434 2.12655
1.87806-0.272145-0.72504-0.256478-0.695652 0.790323 2.12614
1.87838-0.268462-0.722721-0.259371-0.697306 0.790233 2.1258
1.87863-0.265328-0.720385-0.261833-0.699012 0.79016 2.12553
1.87884-0.262746-0.71803-0.263865-0.700766 0.790102.12532
1.879-0.260714-0.715653-0.265462-0.702564 0.790055 2.12514
1.87913-0.259235-0.713253-0.266623-0.704399 0.790017 2.125
1.87925-0.258306-0.710827-0.267346-0.706268 0.789985 2.12488
1.87935-0.257929-0.708373-0.26763-0.708165 0.789955 2.12477
1.87945-0.258103-0.705889-0.267473-0.710086 0.789926 2.12466
1.87957-0.258825-0.703373-0.266876-0.712025 0.789893 2.12454
1.87971-0.260095-0.700823-0.265836-0.713978 0.789853 2.12439
1.87987-0.261911-0.698236-0.264354-0.71594 0.789805 2.12422
1.88008-0.264272-0.695611-0.26243-0.717906 0.789745 2.12399
1.88035-0.267173-0.692945-0.260063-0.719871-0.78967 2.12371
1.88067-0.270613-0.690236-0.257255-0.72183 0.789576 2.12336
1.88107-0.274589-0.687483-0.2540006-0.723778 0.789462 2.12294
1.88155-0.279096-0.684683-0.250318-0.72571 0.789324 2.12243
1.88213-0.284131-0.681835-0.24619-0.727621 0.789159 2.12182
1.8828-0.289691-0.678936-0.241626-0.729507-0.788964 2.12109
1.8836-0.29577-0.675984-0.236626-0.731362 0.788736 2.12025
1.88451-0.302363-0.672978-0.231194-0.733181 0.788473 2.11927
1.88556-0.309466-0.669916-0.22533-0.734959 0.788171 2.11815
1.88675-0.317072-0.666795-0.219039-0.736692 0.787828 2.11688
1.88809-0.325177-0.663613-0.212322-0.738375 0.78744 2.11544
1.88959-0.333773-0.66037-0.205184-0.740002 0.787004 2.11383
1.89127-0.342855-0.657062-0.197626-0.741568-0.786518 2.11203


第一条线是时间,第二和第三条线是第一个粒子的x和y位置,第二和第三个粒子依此类推。因此,我试图实现的目标基本上是使用这个输出文件制作一部电影,显示粒子在正确时间内的运动(在电影中时间必须以相同的速率流动)。有人知道如何做这样的事情吗?

下面的代码应该可以做你想做的事情。基本思想是从模拟输出中插值坐标,以便可以以相等的时间步长绘制坐标

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy.interpolate import interp1d

class AnimatedScatter(object):
    def __init__(self, t, xpositions, ypositions):
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111)
        self.sim_data = np.dstack((xpositions, ypositions))
        self.t = t

        nframes = len(t)

        self.ani = animation.FuncAnimation(self.fig, self.update,\
                                           frames=np.arange(nframes),\
                                           init_func=self.setup_plot)

    def setup_plot(self):
        self.scat = self.ax.scatter(self.sim_data[0,:,0], self.sim_data[0,:,1])
        self.fig.suptitle("t = %f" % (self.t[0]))
        return [self.scat]

    def update(self, i):
        self.scat.set_offsets(self.sim_data[i])
        self.fig.suptitle("t = %f" % (self.t[i]))
        return [self.scat]

    def show(self):
        plt.show()

data = np.genfromtxt("data.txt")

tsim = data[:,0]
xsim = data[:,1::2]
ysim = data[:,2::2]

nframes = 100
nparts = xsim.shape[1]

tplot = np.linspace(tsim[0], tsim[-1], nframes)

xplot = np.empty( (nframes, nparts) )
yplot = np.empty( (nframes, nparts) )

for i in xrange( nparts ):
    xspl = interp1d(tsim, xsim[:,i])
    yspl = interp1d(tsim, ysim[:,i])

    xplot[:,i] = xspl(tplot)
    yplot[:,i] = yspl(tplot)

a = AnimatedScatter(tplot, xplot, yplot)
a.show()

我建议添加一个输出文件行的示例,以帮助人们更好地了解问题。谢谢您的提示,我刚刚添加了一个示例。