Chr*_*ann 6 python animation annotations 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()
Run Code Online (Sandbox Code Playgroud)
你能帮我吗?
我的下一步是:我有这些点的起源向量.这些向量在每个动画步骤中改变它们的长度和方向.我该如何为这些动画制作动画?
没有动画,这有效:
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)
Run Code Online (Sandbox Code Playgroud)
首先,非常感谢您快速而有用的答案!
我解决了我的矢量动画问题:
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="->"))
Run Code Online (Sandbox Code Playgroud)
在'update-function'中我写道:
annotation.xytext = (newData[0][2], newData[1][2])
annotation.xy = (data[0][2]+num, data[1][2]+num)
Run Code Online (Sandbox Code Playgroud)
更改向量的开始和结束位置(箭头).
但是,我有100个载体或更多?写下来是不切实际的:
annotation1 = ...
annotation2 = ...
.
:
annotation100 = ...
Run Code Online (Sandbox Code Playgroud)
我尝试了一个列表:
...
annotation = [annotation1, annotation2, ... , annotation100]
...
def update(num):
...
return line, annotation
Run Code Online (Sandbox Code Playgroud)
并得到此错误: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()
Run Code Online (Sandbox Code Playgroud)
结果看起来像这样:
在这情况下,重要的版本,这个代码在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
Run Code Online (Sandbox Code Playgroud)
您可以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()
Run Code Online (Sandbox Code Playgroud)