python - Emacs:终止正在运行的 python 脚本

标签 python emacs matplotlib

我不知道这是否是 matplotlib/python 的错误,但从 emacs 运行以下命令会消除我通过单击图形窗口上的 [x] 来终止 matplotlib 进程的能力,它没有任何效果。是否有一个命令(我用谷歌搜索过,没有运气)来终止 emacs 启动的特定进程?我可以通过查找缓冲区并执行 C-x k 来终止该进程,但这有点麻烦,有什么方法可以终止所有正在运行的 python 进程吗?

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

最佳答案

C-c C-\ 将使用 SIGQUIT 终止程序,但这不是一个优雅的方式 结束程序。

或者,如果将后端更改为 TkAggC-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 框架,并将 GUI 窗口嵌入到 matplotlib FigureCanvas 中。


这是一个使用 Tk 的示例:

"""
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()

关于python - Emacs:终止正在运行的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660042/

相关文章:

emacs - Option 或 Command 键作为 Macintosh 上 LispBox 的元键

python - 如何显示 matplotlib 图?

python - plt.close() 和 plt.clf() 之间的区别

python - 断言错误 : `create()` did not return an object instance

Python:当 cls 不是 float 时,为什么 float.__new__(cls) 可以工作?

variables - 在Emacs中,如何确保可以在文件中为所有可能的值设置局部变量的安全性

Emacs - *Backtrace* 出错时不会打开

python - 有人在 IronPython 中使用过 SciPy 吗?

python - Django:用户登录系统与用户注册不在同一页面中工作

python-3.x - Python 中 Matplotlib 中的响应式文本