Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:15:46+00:00 2026-06-15T07:15:46+00:00

I don’t know if this is a bug with matplotlib/python but running the following

  • 0

I don’t know if this is a bug with matplotlib/python but running the following from emacs removes my ability to kill the matplotlib process by clicking [x] on the figure window, it has no effect. Is there a command (I googled, no luck) for terminating a specific process that emacs has launched? I can kill the process by finding the buffer and doing C-x k but that is a bit of a hassle, any way to kill ALL running python processes?

#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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:15:48+00:00Added an answer on June 15, 2026 at 7:15 am

    C-c C-\ will kill the program with a SIGQUIT, but that is not a graceful way
    to end the program.

    Alternatively, if you change the backend to TkAgg, C-c
    C-c
    will also terminate the program (again ungracefully), but trying to close the window will still not work.

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

    A full, robust solution requires removing plt.ion(), using a GUI framework like Tk, pygtk, wxpython or pyqt, and embedding the GUI window in a matplotlib FigureCanvas.


    Here is an example using 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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know why, but this code worked for me a month ago... maybe
Don't know if this is the right place to ask this, but I will
Don't know where else to ask, but from one day to the other my
Don't know why this is happening, but after submitting a form via JS (using
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if I'm over-thinking this or not.. but I'm trying to be able
Don't know why but I can't find a solution to this. I have 3
Don't know what is causing this but I try to get the level which
don't know better title for this, but here's my code. I have class user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.