【问题标题】:Animating a moving dot [closed]动画移动点[关闭]
【发布时间】:2016-02-26 17:51:02
【问题描述】:

我需要为一个随机移动的点/图设置动画,直到它穿过一个圆圈,然后它会停留在圆圈上。理想情况下,将有几个点,当最终在圆上时,它们将均匀分布在所述圆上。 我见过使用 matplotlib 和 matplotlib.animate 的代码,它们对函数的图形进行动画处理,但我无法完全理解它们。

感谢您的宝贵时间!

编辑: This is what it would look like with one dot

And with several dots

【问题讨论】:

  • 这可能会有所帮助,如果您可以说明您已经尝试过的内容和/或您不了解的内容,那么它可能会有所帮助。那时提供帮助应该更容易。 :)
  • 嗯,例如,这里有这个教程:jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial 当作者对多个点进行动画处理时(第三个动画),究竟是哪里生成的不仅仅是一个点,而是说 100 个点?

标签: python python-3.x matplotlib


【解决方案1】:

我从来没有在 Python 中制作过任何动画,所以我认为我可以学习一些新的东西,并编写了以下代码来动画一个点,所以它在随机方向上移动。也许它会帮助你开始你的任务。代码基于matplotlib-animation-tutorial

编辑:我有一些长途火车旅行,我已经实现了你所要求的东西。这里有一堆随机移动的点。一旦他们进入蓝色圆圈,他们就会尝试最大化他们之间的距离,所以一段时间后看起来就像你想要的那样。

"""
Matplotlib Animation Example

author: Jake Vanderplas
email: vanderplas@astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import math

# Initializing number of dots
N = 25


# Creating dot class
class dot(object):
    def __init__(self):
        self.x = 10 * np.random.random_sample()
        self.y = 10 * np.random.random_sample()
        self.velx = self.generate_new_vel()
        self.vely = self.generate_new_vel()

    def generate_new_vel(self):
        return (np.random.random_sample() - 0.5) / 5

    def move(self):
        def distance(x1, y1, x2, y2):
            return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

        def inside(x1, y1):
            if distance(x1, y1, 5, 5) <= 1:
                return True
            else:
                return False

        def calc_dist(d):
            ret = 0
            for x in dots:
                if inside(x.x, x.y) and x != d:                            
                    ret = ret + distance(x.x, x.y, d.x, d.y)
            return ret

        # if dot is inside the circle it tries to maximize the distances to
        # other dots inside circle
        if inside(self.x, self.y):
            dist = calc_dist(self)
            for i in xrange(1, 10):
                self.velx = self.generate_new_vel()
                self.vely = self.generate_new_vel()
                self.x = self.x + self.velx
                self.y = self.y + self.vely
                if calc_dist(self) <= dist or not inside(self.x, self.y):
                    self.x = self.x - self.velx
                    self.y = self.y - self.vely
        else:
            if np.random.random_sample() < 0.95:
                self.x = self.x + self.velx
                self.y = self.y + self.vely
            else:
                self.velx = self.generate_new_vel()
                self.vely = self.generate_new_vel()
                self.x = self.x + self.velx
                self.y = self.y + self.vely
            if self.x >= 10:
                self.x = 10
                self.velx = -1 * self.velx
            if self.x <= 0:
                self.x = 0
                self.velx = -1 * self.velx
            if self.y >= 10:
                self.y = 10
                self.vely = -1 * self.vely
            if self.y <= 0:
                self.y = 0
                self.vely = -1 * self.vely

# Initializing dots
dots = [dot() for i in xrange(N)]

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
d, = ax.plot([dot.x for dot in dots],
             [dot.y for dot in dots], 'ro')
circle = plt.Circle((5, 5), 1, color='b', fill=False)
ax.add_artist(circle)


# animation function.  This is called sequentially
def animate(i):
    for dot in dots:
        dot.move()
    d.set_data([dot.x for dot in dots],
               [dot.y for dot in dots])
    return d,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, frames=200, interval=20)

plt.show()

【讨论】:

  • 非常感谢。剩下的问题很少;您能否进一步解释一下: dot.get_xdata()[0] + random.uniform(-0.05, 0.05) 以及为什么“点”后面有逗号?另外,我如何从这个提升到几个移动点?
  • random.uniform(-0.05, 0.05) 返回-0.050.05 之间的随机数,然后添加到点的x 参数中; dot, 是一个元组 (dot,);我认为您可以通过将参数 x, y 添加到列表 ax.plot([5], [5], 'ro') 来添加更多点,但我还不确定,也许我稍后会在此代码上做更多工作。
  • @user5987384 我已经编辑了答案。现在你有多个移动点,它们会像你想要的那样留在蓝色圆圈内。
  • 哇,非常感谢!您的代码完全符合我的要求!虽然有很多我不理解的东西(例如类和自我。),但我会尽我所能通过做一些研究来掌握它的良好理解。
  • 这段代码帮助我开始理解 Python 中的面向对象编程。尚不清楚的一件事是函数 calc_dist :为什么要使用命令 list(dots) 和变量 ret?在我正在编写的另一个程序中,我需要获取有关放置在某个点周围的点的信息,这就是它对我有很大帮助的原因。
猜你喜欢
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 2011-09-12
相关资源
最近更新 更多