2016-08-17 2 views
0

Ich habe einige C++ - Code, der Trajektorien von Partikeln generiert. ich. e. x und y-Koordinaten von Partikeln in verschiedenen Schritten von Simulationen, in denen sieht die Ausgabe-Datei wie folgt (für 4-Partikel):Erstellen einer Animation aus einer Datendatei mit Matplotlib

#step0 
1 2 
3 4 
5 6 
7 8 
#step1 
1.2 2.2 
3.2 4.3 
5.2 6.4 
7.2 8.5 
... 

Ich mag eine Animation mit matplotlib machen, indem Daten aus dieser Datei zu lesen. Dafür habe ich einen Python-Code geändert, aber es erzeugt nur ein leeres Bild.

#!/usr/bin/python 
""" 
Animation of Elastic collisions with Gravity 

author: Jake Vanderplas 
email: [email protected] 
website: http://jakevdp.github.com 
license: BSD 
Please feel free to use and modify this, but keep the above information. Thanks! 
""" 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

N_step = 2 
N_particle = 4 

class ParticleBox: 
    """Orbits class 

    init_state is an [N x 4] array, where N is the number of particles: 
     [[x1, y1, vx1, vy1], 
     [x2, y2, vx2, vy2], 
     ...    ] 

    bounds is the size of the box: [xmin, xmax, ymin, ymax] 
    """ 
    def __init__(self, 
       init_state = [[1, 0, 0, -1], 
           [-0.5, 0.5, 0.5, 0.5], 
           [-0.5, -0.5, -0.5, 0.5]], 
       bounds = [-2, 2, -2, 2], 
       size = 0.04): 
     self.init_state = np.asarray(init_state, dtype=float) 
     self.size = size 
     self.state = self.init_state.copy() 
     self.time_elapsed = 0 
     self.bounds = bounds 

    def step(self, dt): 
     """step once by dt seconds""" 
     self.time_elapsed += dt 
     **x,y = [], []      
     with open("traj.xyz") as f: 
      lines = f.readlines() 

      Data = lines[(N_particle + 1)*self.time_elapsed:N_particle+(N_particle + 1)*self.time_elapsed] 
      for line in Data: 
       row = line.split() 
       x.append(row[0]) 
       y.append(row[1]) 

     # update positions 
     self.state[:, 0] = x 
     self.state[:, 1] = y 

#------------------------------------------------------------ 
# set up initial state 
np.random.seed(0) 
init_state = -0.5 + np.random.random((4, 4)) 

box = ParticleBox(init_state, size=0.04) 
dt = 1. 

#------------------------------------------------------------ 
# set up figure and animation 

fig = plt.figure() 
fig.subplots_adjust(left=0, right=1, bottom=0, top=1) 
axes = fig.add_subplot(111, aspect='equal') 
particles, = axes.plot([], [], 'bo') 

rect = plt.Rectangle(box.bounds[::2], 
        box.bounds[1] - box.bounds[0], 
        box.bounds[3] - box.bounds[2], 
        ec='none', lw=2, fc='none') 
axes.add_patch(rect) 

axes.grid() 
axes.relim() 
axes.autoscale_view(True,True,True) 
#print x,y 

def init(): 
    """initialize animation""" 
    global box, rect 
    particles.set_data([], []) 
    rect.set_edgecolor('none') 
    return particles, rect 

def animate(i): 
    """perform animation step""" 
    global box, rect, dt, axes, fig 
    box.step(dt) 
    # update pieces of the animation 
    rect.set_edgecolor('k') 
    particles.set_data(box.state[:, 0], box.state[:, 1]) 
    particles.set_markersize(10) 
    return particles, rect 

#plt.draw()????? 
ani = animation.FuncAnimation(fig, animate, frames=3, 
           interval=10, blit=True, init_func=init) 
#ani.save('particle_box.mp4', fps=30, extra_args=['-vcodec', 'libx264']) 
plt.show() 

Antwort

0

Sie die Standard Grenzen der Box verwenden, das ist, warum (Ihre Daten sind ähnlich den ersten beiden Datenpunkte unter der Annahme, Sie auf dem Laufenden) nichts zeigt in der Box.

dieses Problem zu beheben, ändern Linie 62 etwas wie folgt aus:

box = ParticleBox(init_state,bounds = [-1, 10, -1, 10], size=0.04)

Verwandte Themen