英文:
How to animate dot over already calculated graph
问题
以下是代码的翻译部分:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
x, y = [], []
line, = plt.plot([], [], 'bo')
circle = plt.Circle((0, 0), 1, color='g', fill=False)
def update(frame):
x.append(np.cos(frame))
y.append(np.sin(frame))
line.set_data(x, y)
return circle, line
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128), interval=0.1)
plt.show()
如果你想要在动画中同时绘制圆圈和点,你可以尝试在update
函数中添加以下代码来绘制圆圈:
circle = plt.Circle((0, 0), 1, color='g', fill=False)
ax.add_patch(circle)
这将在每帧更新时绘制一个固定的圆圈。希望这对你有所帮助。
英文:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
x, y = [], []
line, = plt.plot([], [], 'bo')
circle = plt.Circle((0,0), 1, color = 'g', fill = False,)
def update(frame):
x.append(np.cos(frame))
y.append(np.sin(frame))
line.set_data(x, y)
return circle, line,
ani = FuncAnimation(fig, update, frames= np.linspace(0, 2*np.pi, 128), interval = 0.1)
plt.show()
I tried to animate uniform circular motion through the code above, but what I can see was only dot moving, not the circle under the dot. How can I plot circle while animating dot?
答案1
得分: 1
你可以使用 ax.add_artist(circle)
将圆添加到画家。
另外,我重写了 update
函数,使其仅跟踪当前的点。
参考链接: https://matplotlib.org/stable/api/_as-gen/matplotlib.axes.Axes.add_artist.html
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots(figsize=(5, 5))
radius = 2
ax.set_xlim(-radius * 1.05, radius * 1.05)
ax.set_ylim(-radius * 1.05, radius * 1.05)
line, = plt.plot([], [], 'bo')
circle = plt.Circle((0, 0), radius, color='k', fill=False)
red_dot = plt.plot(0, 0, 'ro')
ax.set_aspect('equal')
ax.add_artist(circle)
ax.set_axis_off()
def update(i):
theta = np.deg2rad(i)
x = [0, radius * np.cos(theta)]
y = [0, radius * np.sin(theta)]
line.set_data(x, y)
ani = FuncAnimation(fig, update, frames=360, interval=30)
ani.save('fig_1.gif', writer='pillow', fps=45)
plt.show()
英文:
You can add the circle to the artist with ax.add_artist(circle)
.
Also, I rewrite the update
function so that it only tracks the current dot.
Reference: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.add_artist.html
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots(figsize=(5, 5))
radius = 2
ax.set_xlim(-radius * 1.05, radius * 1.05)
ax.set_ylim(-radius * 1.05, radius * 1.05)
line, = plt.plot([], [], 'bo')
circle = plt.Circle((0, 0), radius, color='k', fill=False)
red_dot = plt.plot(0, 0, 'ro')
ax.set_aspect('equal')
ax.add_artist(circle)
ax.set_axis_off()
def update(i):
theta = np.deg2rad(i)
x = [0, radius * np.cos(theta)]
y = [0, radius * np.sin(theta)]
line.set_data(x, y)
ani = FuncAnimation(fig, update, frames=360, interval=30)
ani.save('fig_1.gif', writer='pillow', fps=45)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论