使用 matplotlib 为带有标签的点设置动画 [英] Animate points with labels with matplotlib

查看:50
本文介绍了使用 matplotlib 为带有标签的点设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有线条的动画,现在我想标记点.我尝试了 plt.annotate() 并尝试了 plt.text() 但标签不会移动.这是我的示例代码:

I've got an animation with lines and now I want to label the points. I tried plt.annotate() and I tried plt.text() but the labes don't move. This is my example code:

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()

你能帮我吗?

我的下一步是:我在这些点中有原点的向量.这些向量在每个动画步骤中都会改变它们的长度和方向.我如何为这些设置动画?

My next step is: I have vectors with origin in these Points. These vectors change their length and their direction in each animation step. How can I animate these?

没有动画效果:

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)

首先非常感谢您快速且非常有帮助的回答!

First thanks a lot for your fast and very helpful answer!

我用这个解决了我的矢量动画问题:

My Vector animation problem I have solved with this:

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="->"))

在更新函数"中我写道:

and in the 'update-function' I write:

annotation.xytext = (newData[0][2], newData[1][2])
annotation.xy = (data[0][2]+num, data[1][2]+num)

改变向量的开始和结束位置(箭头).

to change the start and end position of the vectors (arrows).

但什么是,当我有 100 个或更多向量时?不可行的写法:

But what is, wehn I have 100 vectors or more? It is not practicable to write:

annotation1 = ...
annotation2 = ...
    .
    :
annotation100 = ...

我尝试使用列表:

...
annotation = [annotation1, annotation2, ... , annotation100]
...

def update(num):
    ...
    return line, annotation

并收到此错误:AttributeError: 'list' 对象没有属性 'axes'

and got this error: AttributeError: 'list' object has no attribute 'axes'

我能做什么?你有什么想法吗?

What can I do? Have you any idea?

推荐答案

您可以返回从更新函数更改的所有对象.因此,由于您的注释更改了它的位置,因此您也应该将其返回:

You have the return all objects that changed from your update function. So since your annotation changed it's position you should return it also:

line.set_data(newData)
annotation = plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
return line, annotation

您可以在 matplotlib 动画的更多信息"noreferrer">本教程

You can read more about matplotlib animations in this tutorial

您还应该指定 init 函数,以便 FuncAnimation 在第一次更新时重绘时知道要从图中删除哪些元素.所以完整的例子是:

You should also specify the init function so that the FuncAnimation knows which elements to remove from the plot when redrawing on the first update. So the full example would be:

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()

这篇关于使用 matplotlib 为带有标签的点设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆