如何在已经计算好的图表上制作点的动画。

huangapple go评论77阅读模式
英文:

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()

what I want to animate

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()

如何在已经计算好的图表上制作点的动画。

huangapple
  • 本文由 发表于 2023年2月6日 14:04:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定