Python 使用matplotlib为带有标签的点设置动画
我有一个带线的动画,现在我想标记点。 我尝试了Python 使用matplotlib为带有标签的点设置动画,python,animation,matplotlib,annotations,Python,Animation,Matplotlib,Annotations,我有一个带线的动画,现在我想标记点。 我尝试了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+
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="->"))
在“更新功能”中,我写道:
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:“列表”对象没有属性“轴”
我能做什么?你知道吗?你有权返回更新函数中更改的所有对象。因此,由于注释更改了它的位置,您也应该返回它:
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语句中返回整个注释列表
然后,重要的是在动画中设置“blit=False”:
animation.FuncAnimation(fig, animate, frames="yourframecount",
interval="yourpreferredinterval", blit=False, init_func=init)
最好指出blit=False可能会减慢速度。但不幸的是,这是我能让列表中的注释动画工作的唯一方法 我从这里来,这里应该更新一个注释,它同时使用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()
导入matplotlib.pyplot作为plt
将numpy作为np导入
将matplotlib.animation导入为动画
图,ax=plt.子批次()
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
L=50
θ=np.linspace(0,2*np.pi,L)
r=np.类(θ)
x=r*np.cos(θ)
y=r*np.sin(θ)
直线,=ax.绘图(1,0,'ro')
annotation=ax.annotate(
“注释”,xy=(1,0),xytext=(-1,0),
arrowprops={'arrowstyle':“->”}
)
def更新(一):
新的_x=x[i%L]
新的_y=y[i%L]
行。设置_数据(新建_x,新建_y)
##annotation.xytext=(-new\u x,-new\u y)您可以使用annotation.xytext=(newData[0][0],newData[1][0])
来更新批注的位置。谢谢@nordev,更新示例。但是为什么annotation.set_位置((newData[0][0],newData[1][0])
不起作用?它被记录为“设置文本的(x,y)位置”xytext
以getter形式记录:-/I使用注释。在我的类的函数中设置_position(),它会正常工作,您使用哪个版本的matplotlib
?也许他们修复了它,或者弄坏了它:我使用matplotlib。uuu version_uuuuu'1.1.1'是的,因为'Annotation'对象没有属性“xytext”
,Annotation.xytext
根本不起作用。@ImportanceOfBeingErnest一个简单的dir(Annotation)
实际上揭示了这一点。我不知道这在哪一点上发生了变化。显然,在旧版本中,它仍然存在。另一件值得注意的事情是,blit=True
在窗口模式下不会产生预期的结果。至少对我来说,它显示了一个原始坐标的静态注释和顶部的旋转注释。但是,如果没有遇到性能问题,建议不要使用blitting,因此可能没有问题。当您设置blit=True
时,您需要同时返回两个艺术家,返回行、注释,
@ImportanceOfBeingErnest我实际上是这样做的(我在第一次发布后编辑了代码),但问题仍然存在。不管怎样,这并不困扰我,我只是想指出这一点。根据github的问题,当将动画保存到磁盘时,无论如何每一帧都会重新绘制整个图形。对我来说效果很好。可能又是macos的事了?