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
我试着通过上面的代码来动画匀速圆周运动,但是我只能看到圆点在移动,而看不到圆点下面的圆。我怎么能在动画圆点的同时画圆呢?
1条答案
按热度按时间qyyhg6bp1#
您可以使用
ax.add_artist(circle)
将圆圈添加到艺术家。另外,我重写了
update
函数,使其只跟踪当前点。参考:https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.add_artist.html