Python 如何设置极轴扇区之间的点的动画?
我尝试在极坐标图(例如四分之一圆、饼图)中围绕扇区在拱门上移动的圆设置动画。在下图中,动画将单个圆从点1移动到点2,然后从点2移动到点3,依此类推,直到圆从点5移动到点6 到目前为止,我无法使形状成为一个圆形,并且在扇区之间没有发生移动 经过大量的实验和谷歌搜索,我无法找到任何关于如何在Python 如何设置极轴扇区之间的点的动画?,python,animation,matplotlib,polar-coordinates,Python,Animation,Matplotlib,Polar Coordinates,我尝试在极坐标图(例如四分之一圆、饼图)中围绕扇区在拱门上移动的圆设置动画。在下图中,动画将单个圆从点1移动到点2,然后从点2移动到点3,依此类推,直到圆从点5移动到点6 到目前为止,我无法使形状成为一个圆形,并且在扇区之间没有发生移动 经过大量的实验和谷歌搜索,我无法找到任何关于如何在init()中正确定义patch.center的位置的指针,以及如何在animate()中对其进行更新,以使其按顺序从1移动到6,如上所述。我看到将参数transform=ax.transData.\u b添加
init()
中正确定义patch.center
的位置的指针,以及如何在animate()
中对其进行更新,以使其按顺序从1移动到6,如上所述。我看到将参数transform=ax.transData.\u b
添加到plt.Circle()
使其成为一个圆,但在动画中,我得到了错误ValueError:无法计算快捷方式,因为其他的变换包含一个不可逆的组件。
任何指点都很感激
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_offset()
方法设置圆的中心:
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,
情节是这样的: