Python 设置移动点的动画

Python 设置移动点的动画,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我需要设置一个点/图的动画,该点/图随机移动,直到它穿过一个圆,然后它将保持不变。理想情况下,会有几个点,当最终在圆上时,这些点将均匀分布在所述圆上。 我见过使用matplotlib和matplotlib.animate为函数图形设置动画的代码,但我无法完全理解它们 谢谢你的时间 编辑: 我从未在Python中设置过任何动画,因此我认为我可以学习一些新的东西,并编写了以下代码来设置一个点的动画,使其在随机方向上移动。也许它会帮助你开始你的任务。代码基于 编辑:我有一次长途火车旅行,我已经实现了

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

谢谢你的时间

编辑:


我从未在Python中设置过任何动画,因此我认为我可以学习一些新的东西,并编写了以下代码来设置一个点的动画,使其在随机方向上移动。也许它会帮助你开始你的任务。代码基于

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

"""
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()
“”“
Matplotlib动画示例
作者:杰克·范德普拉斯
电邮:vanderplas@astro.washington.edu
网站:http://jakevdp.github.com
许可证:BSD
请随意使用和修改,但保留以上信息。谢谢!
"""
将numpy作为np导入
从matplotlib导入pyplot作为plt
从matplotlib导入动画
输入数学
#初始化点数
N=25
#创建点类
类点(对象):
定义初始化(自):
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生成新级别(自我):
返回值(np.random.random_sample()-0.5)/5
def移动(自我):
def距离(x1、y1、x2、y2):
返回数学sqrt((x2-x1)**2+(y2-y1)**2)
内部def(x1,y1):

如果距离(x1,y1,5,5)我从来没有在Python中设置过任何动画,所以我认为我可以学习一些新的东西,并编写了以下代码来设置一个点的动画,使其在随机方向上移动。也许它会帮助你开始你的任务。代码基于

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

"""
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()
“”“
Matplotlib动画示例
作者:杰克·范德普拉斯
电邮:vanderplas@astro.washington.edu
网站:http://jakevdp.github.com
许可证:BSD
请随意使用和修改,但保留以上信息。谢谢!
"""
将numpy作为np导入
从matplotlib导入pyplot作为plt
从matplotlib导入动画
输入数学
#初始化点数
N=25
#创建点类
类点(对象):
定义初始化(自):
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生成新级别(自我):
返回值(np.random.random_sample()-0.5)/5
def移动(自我):
def距离(x1、y1、x2、y2):
返回数学sqrt((x2-x1)**2+(y2-y1)**2)
内部def(x1,y1):

如果距离(x1,y1,5,5)可能会有帮助,如果您可以说明您已经尝试了什么和/或它到底是什么,您不理解。那么提供帮助应该更容易了。:)例如,这里有一个教程:当作者制作了许多点的动画(第三个动画)时,生成一个点(比如100个点)的部分到底在哪里?如果你能说明你已经尝试了什么和/或它到底是什么,你可能会有所帮助,但你不明白。那么提供帮助应该更容易了。:)例如,这里有一个教程:当作者为许多点制作动画(第三个动画)时,确切地说,生成一个点的部分在哪里,比如说100个点?非常感谢。剩下的问题很少;您能否进一步解释一下:dot.get_xdata()[0]+random.uniform(-0.05,0.05)以及为什么在“dot”后面有逗号?另外,我如何从这个点升级到几个移动点?
随机。均匀(-0.05,0.05)
返回介于
-0.05
0.05
之间的随机数,然后将其添加到点的
x
参数中<代码>点,
是一个元组
(点,)
;我想您可以通过在列表中添加参数
x,y
来添加更多的点
ax.plot([5],[5],'ro')
,但我还不确定,也许我以后会在这段代码上做更多的工作。@user5987384我已经编辑了答案。现在你们有了多个移动的点,它们会像你们想要的一样留在蓝色的圆圈里。哇,非常感谢你们!你的代码正是我想要的!虽然有很多事情我不理解(例如类和self),但我会通过做一些研究来尽力理解它。这段代码帮助我开始理解Python中的面向对象编程。还有一件事尚不清楚,那就是calc_dist函数:为什么要使用命令列表(dots)和变量ret?在我正在编写的另一个程序中,我需要获得关于某个点周围的点的信息,这就是为什么它会对我有很大帮助。非常感谢。剩下的问题很少;您能否进一步解释一下:dot.get_xdata()[0]+random.uniform(-0.05,0.05)以及为什么在“dot”后面有逗号?另外,我如何从这个点升级到几个移动点?
随机。均匀(-0.05,0.05)
返回介于
-0.05
0.05
之间的随机数,然后将其添加到点的
x
参数中<代码>点,
是一个元组
(点,)
;我想您可以通过在列表中添加参数
x,y
来添加更多的点
ax.plot([5],[5],'ro')
,但我还不确定,也许我以后会在这段代码上做更多的工作。@user5987384我已经编辑了答案。现在你有多个移动的点和t