【发布时间】:2020-08-12 21:16:13
【问题描述】:
我正在尝试构建散点图,但对动画感到困惑。最终,我希望有 >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)
如何修复此代码?
【问题讨论】:
-
你的
update不见了 - 你有这个吗? -
恐怕我没有。我发现更新非常令人困惑(可能是为什么我的代码不起作用!)我将如何在循环中表达它?
标签: python matplotlib animation