从XY坐标列表中对注释进行动画处理 [英] Animate a annotation from a list of XY Coordinates

查看:130
本文介绍了从XY坐标列表中对注释进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为xy坐标列表中的注释设置动画。下面的代码为 annotation 行设置了动画,但是我无法使用相同的代码使 arrow 函数进行动画处理。

I am trying to animate annotations from a list of xy coordinates. The code below animates the annotation line but I cannot get the arrow function to animate using the same code.

示例数据集表示我正在使用的数据。它是水平格式化的。这样,我就列出了每个主题的所有X坐标和所有Y坐标。然后,我列出与每个时间点有关的列表,每个时间点都是数据的每一行。由此,我可以绘制散点图和注释。

The example dataset is a representation of the data I'm using. It is horizontally formatted. With this, I make a list from all the X-Coordinates and all the Y-Coordiantes from each subject. I then make a list pertaining to each time point, which is each row of data. From that I can plot a scatter and annotations.

但是,当试图在两个单独的坐标之间绘制箭头时,遇到了@ImportanceOfBeingErnest所说的错误。该功能应该是两个元素的元组,但是我遇到了箭头动画功能的麻烦,因为我认为我需要提供4个元素。第一个点的X和Y坐标,第二个点的X和Y坐标。

However, when trying to plot an arrow between two separate coordinates I run into the error as stated by @ImportanceOfBeingErnest. The function should be a tuple of two elements but I'm having trouble with the arrow animation function as I think I need to provide 4 elements. The X and Y coordinate for the first point and X and Y coordinate for the second point.

我是否需要重新格式化该数据,或者是否有一种方法可以动画化箭头功能(需要4个元组)?

Will I have to re-format that data or is there a way to animate the arrow function were 4 tuples are required?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x_data = np.random.randint(80, size=(400, 4))
y_data = np.random.randint(80, size=(400, 4))

lists = [[],[]]
lists[0] = x_data
lists[1] = y_data 

fig, ax = plt.subplots(figsize = (8,6))
ax.set_xlim(0,80)
ax.set_ylim(0,80)

scatter = ax.scatter(lists[0][0], lists[1][0], zorder = 5) #Scatter plot

annotation = ax.annotate('  Subject 1', xy=(lists[0][0][2],lists[1][0][2]))

arrow = ax.annotate('', xy = (lists[0][0][0], lists[1][0][0]), xytext = (lists[0][0][1],lists[1][0][1]), arrowprops = {'arrowstyle': "<->"})

def animate(i) : 
    scatter.set_offsets([[[[[lists[0][0+i][0], lists[1][0+i][0]], [lists[0][0+i][1], lists[1][0+i][1]], [lists[0][0+i][2], lists[1][0+i][2]], [lists[0][0+i][3], lists[1][0+i][3]]]]]])
    Subject_x = lists[0][0+i][2]
    Subject_y = lists[1][0+i][2]
    annotation.set_position((Subject_x, Subject_y))
    annotation.xy = (Subject_x, Subject_y)
    Arrow1 = (lists[0][0+i][0], lists[1][0+i][0]) #Code for arrow animation doesn't work. Produces a blank screen after the 1st frame
    Arrow2 = (lists[0][0+i][1], lists[1][0+i][1])
    arrow.set_position((Arrow1, Arrow2))
    arrow.xy = (Arrow1, Arrow2)

ani = animation.FuncAnimation(fig, animate,
                          interval = 50, blit = False)

plt.show()


推荐答案

这个问题仍然提供了解决方案:
使用带有mathplotlib标签的动画点

The solution to this is still given in this question: Animate points with labels with mathplotlib

正如评论中多次提到的那样,每个位置由两个值<$ c $的元组确定c>(x,y)。因此,您不能为这些位置提供元组。箭头的起点和终点当然也应该在不同的位置。

As said several times in the comments, each position is determined by a tuple of two values (x,y). Hence you cannot provide a tuple of tuples to those positions. Also the start and end of the arrow should of course be at different positions.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x_data = np.random.randint(80, size=(400, 4))
y_data = np.random.randint(80, size=(400, 4))


fig, ax = plt.subplots(figsize = (8,6))
ax.set_xlim(0,80)
ax.set_ylim(0,80)

scatter = ax.scatter(x_data[0], y_data[0], zorder = 5) #Scatter plot

annotation = ax.annotate('  Subject 1', xy=(x_data[0,2],y_data[0,2]))

arrow = ax.annotate('', xy = (x_data[0,0], y_data[0,0]), 
                        xytext = (x_data[0,1],y_data[0,1]), 
                        arrowprops = {'arrowstyle': "<->"})

def animate(i) : 
    scatter.set_offsets(np.c_[x_data[i,:], y_data[i,:]])

    annotation.set_position((x_data[i,2], y_data[i,2]))

    start = (x_data[i,0], y_data[i,0]) 
    end   = (x_data[i,1], y_data[i,1])
    arrow.set_position(start)
    arrow.xy = end

ani = animation.FuncAnimation(fig, animate, frames=len(x_data), 
                              interval = 700, blit = False)

plt.show()

这篇关于从XY坐标列表中对注释进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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