如何为极轴扇区之间的点设置动画?
how can I animate points between sectors of polar axis?
我正在尝试为极坐标图中的扇区(例如,四分之一圆、饼图)的圆弧移动设置动画。在下图中,动画将从点 1 移动到点 2,然后从点 2 移动到点 3,依此类推,直到圆从点 5 移动到点 6。
到目前为止,我无法使形状成为圆形,并且扇区之间没有发生移动。
经过多次试验和谷歌搜索,我无法找到任何关于如何在 init()
中适当地定义 patch.center
的位置以及如何在 [=13= 中更新它的指示] 以便它按顺序从 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()
改为如下代码,使用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,
剧情是这样的:
我正在尝试为极坐标图中的扇区(例如,四分之一圆、饼图)的圆弧移动设置动画。在下图中,动画将从点 1 移动到点 2,然后从点 2 移动到点 3,依此类推,直到圆从点 5 移动到点 6。
到目前为止,我无法使形状成为圆形,并且扇区之间没有发生移动。
经过多次试验和谷歌搜索,我无法找到任何关于如何在 init()
中适当地定义 patch.center
的位置以及如何在 [=13= 中更新它的指示] 以便它按顺序从 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()
改为如下代码,使用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,
剧情是这样的: