[英]Animate points with labels with matplotlib
我有一个带线条的动画,现在我想标记点数。 我尝试了plt.annotate()
,我尝试了plt.text()
但实验室不动。 这是我的示例代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, data, line):
newData = np.array([[1+num,2+num/2,3,4-num/4,5+num],[7,4,9+num/3,2,3]])
line.set_data(newData)
plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
return line,
fig1 = plt.figure()
data = np.array([[1,2,3,4,5],[7,4,9,2,3]])
l, = plt.plot([], [], 'r-')
plt.xlim(0, 20)
plt.ylim(0, 20)
plt.annotate('A0', xy=(data[0][0], data[1][0]))
# plt.text( data[0][0], data[1][0], 'A0')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=200, blit=True)
plt.show()
你能帮我吗?
我的下一步是:我有这些点的起源向量。 这些向量在每个动画步骤中改变它们的长度和方向。 我该如何为这些动画制作动画?
没有动画,这有效:
soa =np.array( [ [data[0][0],data[1][0],F_A0[i][0][0],F_A0[i][1][0]],
[data[0][1],data[1][1],F_B0[i][0][0],F_B0[i][1][0]],
[data[0][2],data[1][2],F_D[i][0][0],F_D[i][1][0]] ])
X,Y,U,V = zip(*soa)
ax = plt.gca()
ax.quiver(X,Y,U,V,angles='xy',scale_units='xy',scale=1)
首先,非常感谢您快速而有用的答案!
我解决了我的矢量动画问题:
annotation = ax.annotate("C0", xy=(data[0][2], data[1][2]), xycoords='data',
xytext=(data[0][2]+1, data[1][2]+1), textcoords='data',
arrowprops=dict(arrowstyle="->"))
在'update-function'中我写道:
annotation.xytext = (newData[0][2], newData[1][2])
annotation.xy = (data[0][2]+num, data[1][2]+num)
更改向量的开始和结束位置(箭头)。
但是,我有100个载体或更多? 写下来是不切实际的:
annotation1 = ...
annotation2 = ...
.
:
annotation100 = ...
我尝试了一个列表:
...
annotation = [annotation1, annotation2, ... , annotation100]
...
def update(num):
...
return line, annotation
并得到此错误:AttributeError:'list'对象没有属性'axes'
我能做什么? 你知道吗?
我来自这个问题 ,其中应该更新使用xy
和xytext
。 看来,为了正确更新注释,需要设置注释的属性 .xy
来设置注释点的位置,并使用注释的.set_position()
方法来设置注释的位置。 。 设置.xytext
属性没有任何影响 - 在我看来有点令人困惑。 下面是完整的例子:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
fig, ax = plt.subplots()
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
L = 50
theta = np.linspace(0,2*np.pi,L)
r = np.ones_like(theta)
x = r*np.cos(theta)
y = r*np.sin(theta)
line, = ax.plot(1,0, 'ro')
annotation = ax.annotate(
'annotation', xy=(1,0), xytext=(-1,0),
arrowprops = {'arrowstyle': "->"}
)
def update(i):
new_x = x[i%L]
new_y = y[i%L]
line.set_data(new_x,new_y)
##annotation.xytext = (-new_x,-new_y) <-- does not work
annotation.set_position((-new_x,-new_y))
annotation.xy = (new_x,new_y)
return line, annotation
ani = animation.FuncAnimation(
fig, update, interval = 500, blit = False
)
plt.show()
结果看起来像这样:
如果版本很重要,则此代码已在Python 2.7和3.6上使用matplotlib版本2.1.1进行了测试,并且在两种情况下设置.xytext
都没有效果,而.set_position()
和.xy
按预期工作。
您可以返回从更新函数更改的所有对象。 因此,由于您的注释改变了它的位置,您还应该将其返回:
line.set_data(newData)
annotation = plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
return line, annotation
您可以在本教程中阅读有关matplotlib
动画的更多信息
您还应该指定init
函数,以便FuncAnimation
知道在第一次更新时重绘时要从绘图中删除哪些元素。 所以完整的例子是:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Create initial data
data = np.array([[1,2,3,4,5], [7,4,9,2,3]])
# Create figure and axes
fig = plt.figure()
ax = plt.axes(xlim=(0, 20), ylim=(0, 20))
# Create initial objects
line, = ax.plot([], [], 'r-')
annotation = ax.annotate('A0', xy=(data[0][0], data[1][0]))
annotation.set_animated(True)
# Create the init function that returns the objects
# that will change during the animation process
def init():
return line, annotation
# Create the update function that returns all the
# objects that have changed
def update(num):
newData = np.array([[1 + num, 2 + num / 2, 3, 4 - num / 4, 5 + num],
[7, 4, 9 + num / 3, 2, 3]])
line.set_data(newData)
# This is not working i 1.2.1
# annotation.set_position((newData[0][0], newData[1][0]))
annotation.xytext = (newData[0][0], newData[1][0])
return line, annotation
anim = animation.FuncAnimation(fig, update, frames=25, init_func=init,
interval=200, blit=True)
plt.show()
我想我想出了如何通过列表为多个注释添加动画效果。 首先,您只需创建注释列表:
for i in range(0,len(someMatrix)):
annotations.append(ax.annotate(str(i), xy=(someMatrix.item(0,i), someMatrix.item(1,i))))
然后在你的“动画”功能中,你就像你已经写过的那样:
for num, annot in enumerate(annotations):
annot.set_position((someMatrix.item((time,num)), someMatrix.item((time,num))))
(如果你不喜欢枚举方式,你可以把它写成传统的for循环)。 不要忘记在return语句中返回整个注释列表。
然后重要的是在你的FuncAnimation中设置“blit = False”:
animation.FuncAnimation(fig, animate, frames="yourframecount",
interval="yourpreferredinterval", blit=False, init_func=init)
值得指出的是,blit = False可能会减慢速度。 但不幸的是,这是我能够在列表中获得注释动画的唯一方法......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.