【问题标题】:Matplotlib crude animation, show traceMatplotlib 粗动画,显示痕迹
【发布时间】:2012-11-20 15:05:36
【问题描述】:

我刚刚启动了一个小型粒子模拟器,我希望扩展它以解决一些物理问题,但我在尝试为它们设置动画时遇到了问题。基本上取决于您选择哪种随机分布,粒子将在给定长度的区域中“振荡”。我想在前面的 10 个步骤中展示粒子的“历史”。

这里是代码

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)

我想要的是行更新不会在每次迭代时清除画布并重绘,我只是希望它这样做,例如,每 10 帧(迭代),这将使我更容易跟踪粒子视觉上。

还有一件事我想实现但不是绝对必要的,是否可以在每个“o”周围画一个盒子(正方形)或一个圆形或一个三角形?这样该点以该框/圆/三角形为中心?这将再次使跟踪粒子变得更加容易。如果我能指定哪个“o”(点)获得这个属性(正方形),那就更好了。

【问题讨论】:

    标签: python matplotlib plot particles


    【解决方案1】:

    试试animation module。还可以看到这个很棒的tutorialMatplotlib animate over an image(主要是我对Animate drawing networkx edges的回答的副本和过去)

    要获得所需的时间延迟,您需要设置一个历史数据结构,例如:

    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)
    

    至于盒子,使用RectangleDraw rectangle (add_patch) in pylab mode)和可选参数传递给FuncAnimation

    这并不完美,它有一些奇怪的细节循环回到它自己(一个循环中的第 50 帧与下一次经过的第 50 帧不同),前 10 帧有一个瞬态当历史中有一堆零时(您可以通过将历史数组初始化为初始点的 10 个副本来解决此问题)

    【讨论】:

    • 太棒了!明天将进行测试,希望它有效。
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2013-12-14
    • 2010-11-04
    • 2011-12-30
    • 2020-03-04
    相关资源
    最近更新 更多