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 8886047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:27:46+00:00 2026-06-14T21:27:46+00:00

I am just starting a small particle simulator I wish to expand to solve

  • 0

I am just starting a small particle simulator I wish to expand to solve some physics problems but I have a problem trying to animate them. Essentially depending on what kind of random distribution you choose the particles will “oscillate” in an area of a given length. I want to show the “history” of the particle at say the 10 previous steps.

Here is the code

from numpy import *
import matplotlib.pyplot as plt
import pylab
import time

pylab.ion()

N = 10
r = random.randint(-100,100,2*N).reshape(N,2)
line, = plt.plot(r[:,0], r[:,1],'o')

for i in range(100000):
  newdatax = r[:,0] + random.rand(N)
  newdatay = r[:,1] + random.rand(N)
  line.set_ydata(newdatay)
  line.set_xdata(newdatax)
  plt.title("At timestep: %d" %i)
  plt.hold(True)
  plt.draw()
  time.sleep(1.0/30)

What I want is for the line update to NOT clear the canvas and redraw at every iteration, I just want it to do that for say, every 10th frame (iteration), this will make it easier for me to track the particles visually.

There is another thing I wish to implement but it is not strictly necessary, is it possible to draw a box (square) or a circle or a triangle around each “o”? Such that the point is centered in that box/circle/triangle? This would again make it much easier to track particles. It would be even nicer if I could dictate which “o” (point) gets this property (square).

  • 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-14T21:27:48+00:00Added an answer on June 14, 2026 at 9:27 pm

    Try the animation module. Also see this awesome tutorial, Matplotlib animate over an image (mostly a copy and past of my answer to Animate drawing networkx edges)

    To get the time lag you want, you need to set up a history data structure, something like:

    from matplotlib import animation
    
    fig = figure()
    N = 10
    r = random.randint(-100,100,2*N).reshape(N,2)
    line, = plt.plot(r[:,0], r[:,1],'o')
    
    
    lag_len = 10
    history_x = np.zeros((N,lag_len))
    history_y = np.zeros((N,lag_len))
    trails = [plot(hx,hy)[0] for hx,hy in zip(history_x,history_y)]
    
    def update_frame(i):
        frame_num = i
        newdatax = r[:,0] + random.rand(N)
        newdatay = r[:,1] + random.rand(N)
        line.set_ydata(newdatay)
        line.set_xdata(newdatax)
        history_x[:,frame_num%lag_len] = newdatax
        history_y[:,frame_num%lag_len] = newdatay
        for hx,hy,ln_h in zip(history_x,history_y,trails):
             ln_h.set_xdata(hx)
             ln_h.set_ydata(hy)
    
        plt.title("At timestep: %d" %i)
        plt.hold(True)
        return (line,) + tuple(trails)
    
    anim = animation.FuncAnimation(fig, update_frame, 
                               frames=100, interval=20)
    

    As for the boxes, use Rectangle ( Draw rectangle (add_patch) in pylab mode ), and the optional args to pass to FuncAnimation.

    This isn’t perfect, there are some strange details of it looping back over it’s self (frame 50 in one loop is not the same as frame 50 next time it passes over), there is a transient for the first 10 frames when there are a bunch of zeros in the history (you can fix that by initializing the history array to be 10 copies of the initial points)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting with BDD and I'm trying to build a small app, so
We have a 'small scale' web app. Actually just starting up. For devs already
This is a trivial problem, but I'm just starting with matlab, and haven't got
I just starting a project, I would like to have a small content manager
Just starting with FactoryGirl. I have a Model named Subscription . It has a
Just starting out with subversion, have set up repos for 3 current projects and
Just starting out, this should be a simple one but I haven't been able
I am just starting out with writing jQuery plugins. I wrote three small plugins
I'm just starting off with C and was trying out a few examples from
I'm just starting out with unit testing in C#. I have been reading about

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.