我正在尝试构建一个散点图并且对动画感到困惑。最终,我希望有 >1000 个点,以 (0,0) 附近的随机正态分布绘制。然后每个点应以不同的速度围绕 (0,0) 顺时针或逆时针旋转。
我试图用一个可以随机移动的循环函数来做到这一点,但它非常慢,而且到目前为止我还无法进行超过一个点的移动:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 4,3
from matplotlib.animation import FuncAnimation
from math import sqrt, exp
from matplotlib.animation import FuncAnimation, PillowWriter
fig, ax = plt.subplots()
# set the axes limits
ax.axis([-2.5,2.5,-2.5,2.5])
# set equal aspect such that the circle is not shown as ellipse
ax.set_aspect("equal")
# create a point in the axes
for i in range(0,6):
x = np.random.normal()
y = np.random.normal()
r = np.sqrt(x*x+y*y)
def circle(phi):
return np.array([r*np.cos(phi), r*np.sin(phi)])
point, = ax.plot(x,y, marker="o")
ani = FuncAnimation(fig, update, interval=10, blit=True, repeat=True,
frames=np.linspace(0,2*np.pi,360, endpoint=False))
plt.show()
writer = PillowWriter(fps=25)
ani.save(r'[path --> .gif]', writer=writer)
如何修复此代码?
