我试图在极坐标图中的扇区周围的弧上运动一个圆圈(例如圆的四分之一,饼图)。在下图中,动画将从1点移动到2,然后从2移动到3,依此类推,直到圆圈从5移动到6。
到目前为止,我无法将形状变成圆形,并且不会在各个扇区之间发生运动。
经过大量的实验和谷歌搜索,我无法找到关于如何恰当地定义patch.center
中init()
的位置的任何指示,以及如何在animate()
中更新它如上所述,它按顺序从1到6传播。我在this post中看到,将参数transform=ax.transData._b
添加到plt.Circle()
会使其成为一个圆圈,但在动画中我得到错误ValueError: The shortcut cannot be computed since other's transform includes a non-invertable component.
。
任何指针都表示赞赏!
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()