从 XY 坐标列表动画注释 [英] Animate a annotation from a list of XY Coordinates

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

问题描述

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

示例数据集代表了我正在使用的数据.它是水平格式的.有了这个,我从每个主题的所有 X 坐标和所有 Y 坐标中列出了一个列表.然后我制作一个与每个时间点有关的列表,即每一行数据.从中我可以绘制散点图和注释.

但是,当尝试在两个单独的坐标之间绘制箭头时,我遇到了@ImportanceOfBeingErnest 所述的错误.该函数应该是一个包含两个元素的元组,但是我在使用箭头动画函数时遇到了问题,因为我认为我需要提供 4 个元素.第一个点的 X 和 Y 坐标和第二个点的 X 和 Y 坐标.

如果需要 4 个元组,我是否必须重新格式化该数据,或者有没有办法为箭头函数设置动画?

将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt导入 matplotlib.animation 作为动画x_data = np.random.randint(80, size=(400, 4))y_data = np.random.randint(80, size=(400, 4))列表 = [[],[]]列表[0] = x_data列表[1] = y_data图, 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) #散点图annotation = ax.annotate('Subject 1', xy=(lists[0][0][2],lists[1][0][2]))箭头 = ax.annotate('', xy = (lists[0][0][0],lists[1][0][0]),xytext = (lists[0][0][1],lists[1][0][1]), arrowprops = {'arrowstyle': "<->"})定义动画(i):scatter.set_offsets([[[[[lists[0][0+i][0],lists[1][0+i][0]],[lists[0][0+i][1],列表[1][0+i][1]]、[列表[0][0+i][2]、列表[1][0+i][2]]、[列表[0][0+]i][3],列表[1][0+i][3]]]]]])Subject_x = 列表[0][0+i][2]Subject_y = 列表[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]) #箭头动画代码不起作用.在第一帧之后产生一个空白屏幕Arrow2 = (lists[0][0+i][1],lists[1][0+i][1])arrow.set_position((Arrow1, Arrow2))箭头.xy =(箭头1,箭头2)ani = animation.FuncAnimation(无花果,动画,间隔 = 50,blit = 假)plt.show()

解决方案

这个问题的解决方案还在这个问题里:使用 mathplotlib 为带有标签的点制作动画>

正如评论中多次提到的,每个位置由两个值的元组 (x,y) 决定.因此,您不能为这些位置提供元组元组.此外,箭头的起点和终点当然应该在不同的位置.

将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt导入 matplotlib.animation 作为动画x_data = np.random.randint(80, size=(400, 4))y_data = np.random.randint(80, size=(400, 4))图, 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) #散点图annotation = ax.annotate('Subject 1', xy=(x_data[0,2],y_data[0,2]))箭头 = ax.annotate('', xy = (x_data[0,0], y_data[0,0]),xytext = (x_data[0,1],y_data[0,1]),arrowprops = {'arrowstyle': "<->"})定义动画(i):scatter.set_offsets(np.c_[x_data[i,:], y_data[i,:]])annotation.set_position((x_data[i,2], y_data[i,2]))开始 = (x_data[i,0], y_data[i,0])结束 = (x_data[i,1], y_data[i,1])arrow.set_position(start)箭头.xy = 结束ani = animation.FuncAnimation(fig, animate, frames=len(x_data),间隔 = 700,blit = 假)plt.show()

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.

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.

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.

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

解决方案

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

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