使用matplotlib使用标签对点进行动画处理 [英] Animate points with labels with matplotlib

查看:172
本文介绍了使用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?

在没有动画的情况下有效:

Without animation this works:

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!

我已经解决的Vector动画问题:

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:列表"对象没有属性轴"

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动画的信息.教程

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天全站免登陆