2012-12-01 3 views
2

이것이 matplotlib/python의 버그인지는 모르겠지만 emacs에서 다음을 실행하면 Figure 창에서 [x]를 클릭하여 matplotlib 프로세스를 죽일 수있는 능력이 제거됩니다. 효과. 이맥스 (emacs)가 시작한 특정 프로세스를 종료하기위한 명령이 있습니까 (I googled, no luck)? 버퍼를 찾아서 C-x k을 수행하여 프로세스를 종료 할 수 있습니다.하지만 모든 번거로운 파이썬 프로세스를 죽일 수있는 번거로운 방법일까요?이맥스 : 실행중인 파이썬 스크립트를 죽이기

#Simple circular box simulator, part of part_sim 
#Restructure to import into gravity() or coloumb() or wind() or pressure() 
#Or to use all forces: sim_full() 
#Note: Implement crashing as backbone to all forces 
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.spatial.distance import pdist, squareform 

N = 100            #Number of particles 
R = 10000           #Box width 
pR= 5            #Particle radius 

r = np.random.randint(0, R, (N,2))     #Position vector 
v = np.random.randint(-R/100,R/100,(N,2))   #velocity vector 
a = np.array([0,-10])        #Forces 
v_limit = R/2          #Speedlimit 


plt.ion() 
line, = plt.plot([],'o') 
line2, = plt.plot([],'o')       #Track a particle 
plt.axis([0,R+pR,0,R+pR] 


while True: 

    v=v+a           #Advance 
    r=r+v 

    #Collision tests 
    r_hit_x0 = np.where(r[:,0]<0)     #Hit floor? 
    r_hit_x1 = np.where(r[:,0]>R)     #Hit roof? 
    r_hit_LR = np.where(r[:,1]<0)     #Left wall? 
    r_hit_RR = np.where(r[:,1]>R)     #Right wall? 


    #Stop at walls 
    r[r_hit_x0,0] = 0 
    r[r_hit_x1,0] = R 
    r[r_hit_LR,1] = 0 
    r[r_hit_RR,1] = R 

    #Reverse velocities 
    v[r_hit_x0,0] = -0.9*v[r_hit_x0,0] 
    v[r_hit_x1,0] = -v[r_hit_x1,0] 
    v[r_hit_LR,1] = -0.95*v[r_hit_LR,1] 
    v[r_hit_RR,1] = -0.99*v[r_hit_RR,1] 

    #Collisions 
    D = squareform(pdist(r)) 
    ind1, ind2 = np.where(D < pR) 
    unique = (ind1 < ind2) 
    ind1 = ind1[unique] 
    ind2 = ind2[unique] 

    for i1, i2 in zip(ind1, ind2): 
     eps = np.random.rand() 
     vtot= v[i1,:]+v[i2,:] 
     v[i1,:] = -(1-eps)*vtot 
     v[i2,:] = -eps*vtot 

    line.set_ydata(r[:,1]) 
    line.set_xdata(r[:,0]) 
    line2.set_ydata(r[:N/5,1]) 
    line2.set_xdata(r[:N/5,0]) 
    plt.draw() 

답변

2

C-c C-\는 SIGQUIT와 프로그램을 죽일 것이다, 그러나 그것은 프로그램을 종료하는 우아한 방법 없습니다.

백엔드를 TkAgg으로 변경하면 C-c C-c도 프로그램을 종료 (다시 비정상적으로)하지만 창을 닫으려고해도 계속 작동하지 않습니다.

import numpy as np 
    import matplotlib as mpl 
    mpl.use('TkAgg') # do this before importing pyplot 
    import matplotlib.pyplot as plt 

전체 강력한 솔루션, plt.ion() 제거의 Tk, pygtk, 또는 wxPython에 PyQt는 같은 GUI 프레임 워크를 사용하고,하기 matplotlib의 FigureCanvas있는 GUI 윈도우 매립 요구한다.

""" 
http://stackoverflow.com/q/13660042/190597 
Simple circular box simulator, part of part_sim 
Restructure to import into gravity() or coloumb() or wind() or pressure() 
Or to use all forces: sim_full() 
Note: Implement crashing as backbone to all forces 
""" 

import Tkinter as tk 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.figure as mplfig 
import scipy.spatial.distance as dist 
import matplotlib.backends.backend_tkagg as tkagg 

class App(object): 
    def __init__(self, master): 
     self.master = master 
     self.fig = mplfig.Figure(figsize = (5, 4), dpi = 100) 
     self.ax = self.fig.add_subplot(111) 
     self.canvas = canvas = tkagg.FigureCanvasTkAgg(self.fig, master) 
     canvas.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = 1) 
     self.toolbar = toolbar = tkagg.NavigationToolbar2TkAgg(canvas, master) 
     self.button = button = tk.Button(master, text = 'Quit', command = master.quit) 
     button.pack(side = tk.BOTTOM) 
     toolbar.update() 
     self.update = self.animate().next 
     master.after(10, self.update) 
     canvas.show() 

    def animate(self): 
     N = 100            #Number of particles 
     R = 10000           #Box width 
     pR= 5            #Particle radius 

     r = np.random.randint(0, R, (N,2))     #Position vector 
     v = np.random.randint(-R/100,R/100,(N,2))   #velocity vector 
     a = np.array([0,-10])        #Forces 
     v_limit = R/2          #Speedlimit 

     line, = self.ax.plot([],'o') 
     line2, = self.ax.plot([],'o')       #Track a particle 
     self.ax.set_xlim(0, R+pR) 
     self.ax.set_ylim(0, R+pR)   

     while True: 
      v=v+a           #Advance 
      r=r+v 

      #Collision tests 
      r_hit_x0 = np.where(r[:,0]<0)     #Hit floor? 
      r_hit_x1 = np.where(r[:,0]>R)     #Hit roof? 
      r_hit_LR = np.where(r[:,1]<0)     #Left wall? 
      r_hit_RR = np.where(r[:,1]>R)     #Right wall? 

      #Stop at walls 
      r[r_hit_x0,0] = 0 
      r[r_hit_x1,0] = R 
      r[r_hit_LR,1] = 0 
      r[r_hit_RR,1] = R 

      #Reverse velocities 
      v[r_hit_x0,0] = -0.9*v[r_hit_x0,0] 
      v[r_hit_x1,0] = -v[r_hit_x1,0] 
      v[r_hit_LR,1] = -0.95*v[r_hit_LR,1] 
      v[r_hit_RR,1] = -0.99*v[r_hit_RR,1] 

      #Collisions 
      D = dist.squareform(dist.pdist(r)) 
      ind1, ind2 = np.where(D < pR) 
      unique = (ind1 < ind2) 
      ind1 = ind1[unique] 
      ind2 = ind2[unique] 

      for i1, i2 in zip(ind1, ind2): 
       eps = np.random.rand() 
       vtot= v[i1,:]+v[i2,:] 
       v[i1,:] = -(1-eps)*vtot 
       v[i2,:] = -eps*vtot 

      line.set_ydata(r[:,1]) 
      line.set_xdata(r[:,0]) 
      line2.set_ydata(r[:N/5,1]) 
      line2.set_xdata(r[:N/5,0]) 
      self.canvas.draw() 
      self.master.after(1, self.update) 
      yield 

def main(): 
    root = tk.Tk() 
    app = App(root) 
    tk.mainloop() 

if __name__ == '__main__': 
    main() 
+0

SO 내게가 : 아름다운 물어보다 더주고 그것을두고이 훨씬 더 나를 가르치고, 감사 : 여기


은 Tk의 사용 예입니다! – arynaq

관련 문제