2016-08-17 3 views
0

입자의 궤적을 생성하는 C++ 코드가 있습니다. 나는. 이자형. X 및 시뮬레이션의 상이한 단계에서 입자의 Y 좌표하는 출력 파일 (4 개 입자)과 같다 :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 
... 

는 I이 파일로부터 데이터를 판독하여 하기 matplotlib와 애니메이션을 원한다. 이 일을 위해 파이썬 코드를 변경했으나 빈 이미지 만 생성합니다. 아무것도 상자에 표시되지 않습니다 (데이터가 당신이 게시 처음 두 개의 데이터 포인트와 유사하다 가정) 왜

#!/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() 

답변

0

당신이 상자의 기본 경계를 사용하고, 그입니다.

은 다음과 같이 뭔가 변화 라인, 62 을이 문제를 해결하려면 :

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

관련 문제