我正在尝试为极坐标图中的扇区(例如四分之一圆、饼图)在拱形上移动的圆制作动画。在下图中,动画将从点 1 移动到点 2,然后从点 2 移动到点 3,依此类推,直到圆从点 5 移动到点 6。
到目前为止,我无法使形状成为圆形,并且扇区之间没有发生移动。
经过多次试验和谷歌搜索,我无法找到任何关于如何在 init() 中适本地定义 patch.center 的位置,以及如何在 animate() 中更新它,使其按顺序从 1 移动到 6,如上所述。我在 this post 看到了将参数 transform=ax.transData._b 添加到 plt.Circle() 使其成为一个圆圈,但随后在动画上出现错误 ValueError: The无法计算捷径,因为其他人的变换包含不可逆分量。。
感谢任何指点!
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
r_list = [1.3, 1.3, 1.3, 1.3, 1.3, 1.3]
theta_list = [1.5707963267948966, 0.7853981633974483, -3.9269908169872414, 0.0, 3.141592653589793, -0.7853981633974483]
def cart2pol(x, y):
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x)
return(rho, phi)
def pol2cart(rho, phi):
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(x, y)
fig = plt.figure()
ax = plt.subplot(111, polar=True)
####### used only to clearly label intended travel #######
c = plt.scatter(theta_list, r_list)
ax.set_yticklabels([])
labels=["1", "2", "3", "4", "5", "6"]
for i, txt in enumerate(labels):
ax.annotate(txt, (theta_list[i], r_list[i]))
##########################################################
patch = plt.Circle(pol2cart(r_list[0], theta_list[0]), 0.5, alpha=0.5)
def init():
patch.center = (pol2cart(r_list[0], theta_list[0]))
ax.add_patch(patch)
return patch,
def animate(i):
x, y = patch.center
x, y = (pol2cart(r_list[i%6], theta_list[i%6]))
patch.center = (x, y)
return patch,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=360,
interval=200,
blit=True)
plt.show()
请您参考如下方法:
改为如下代码,使用scatter()绘制圆,通过set_offsets()方法设置圆心:
patch = plt.scatter(theta_list[0], r_list[0], 600, alpha=0.5)
def init():
patch.set_offsets([[theta_list[0], r_list[0]]])
return patch,
def animate(i):
patch.set_offsets([[theta_list[i % 6], r_list[i % 6]]])
return patch,
剧情是这样的:
