Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何设置围绕圆周长移动的点的动画?_Python_Matplotlib - Fatal编程技术网

Python 如何设置围绕圆周长移动的点的动画?

Python 如何设置围绕圆周长移动的点的动画?,python,matplotlib,Python,Matplotlib,使用此代码,如何设置点的动画以跟踪圆 import numpy as np import matplotlib.pyplot as plt x = np.linspace(-1.0, 1.0, 100) y = np.linspace(-1.0, 1.0, 100) X, Y = np.meshgrid(x,y) F = X**2 + Y**2 - 0.6 plt.contour(X,Y,F,[0]) plt.gca().set_aspect('equal') plt.show() 我需要它

使用此代码,如何设置点的动画以跟踪圆

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1.0, 1.0, 100)
y = np.linspace(-1.0, 1.0, 100)
X, Y = np.meshgrid(x,y)
F = X**2 + Y**2 - 0.6
plt.contour(X,Y,F,[0])
plt.gca().set_aspect('equal')
plt.show()

我需要它看起来像什么。(很抱歉,我找不到更好的动画来描述我想要的内容)我描述的点将是月球绕其旋转的圆的中心点。

您需要对圆进行参数化,以便每个时间步在该圆上给出不同的坐标。这可能最好在极坐标系中完成,在极坐标系中,角度直接为您提供要改变的参数

r = 1 # radius of circle
def circle(phi):
    return np.array([r*np.cos(phi), r*np.sin(phi)])
然后需要设置matplotlib图形和轴,并定义更新函数,如果调用该函数,将点的位置设置为从上述
函数接收到的值。 然后,您可以通过
FuncAnimation
为整个过程设置动画,这会反复调用更新函数

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 4,3
from matplotlib.animation import FuncAnimation

r = 1 # radius of circle
def circle(phi):
    return np.array([r*np.cos(phi), r*np.sin(phi)])

# create a figure with an axes
fig, ax = plt.subplots()
# set the axes limits
ax.axis([-1.5,1.5,-1.5,1.5])
# set equal aspect such that the circle is not shown as ellipse
ax.set_aspect("equal")
# create a point in the axes
point, = ax.plot(0,1, marker="o")

# Updating function, to be repeatedly called by the animation
def update(phi):
    # obtain point coordinates 
    x,y = circle(phi)
    # set point's coordinates
    point.set_data([x],[y])
    return point,

# create animation with 10ms interval, which is repeated,
# provide the full circle (0,2pi) as parameters
ani = FuncAnimation(fig, update, interval=10, blit=True, repeat=True,
                    frames=np.linspace(0,2*np.pi,360, endpoint=False))

plt.show()

这是一个非常好的方法,非常有帮助。如果你能评论一下你的代码,那将是非常有帮助的,我一直在尝试弄清楚每件事都能做些什么。不过,如果您很忙或不想,请不要担心itI对代码进行了注释。我认为这很容易解释,所以最好明确地问一下你不明白的地方。你在哪里定义圆的中心点/如果它默认为0,0,怎么可能圆的中心是(0,0)当然你可以移动它,如果你愿意,例如
np.array([r*np.cos(phi),r*np.sin(phi)]+np.array((0.2,-0.1))