我正在尝试注释列表列表的XY坐标的箭头。我可以在某个点显示注释,但是当我尝试在两个XY坐标之间添加箭头时,请获得Type Error:'float' object is not iterable
。
代码如下所示:
import csv
import matplotlib.pyplot as plt
import matplotlib.animation as animation
visuals = [[],[],[],[],[]]
with open('XY_Data.csv') as csvfile :
readCSV = csv.reader(csvfile, delimiter=',')
n=0
for row in readCSV :
if n == 0 :
n+=1
continue
visuals[0].append(list(map(float, row[3:43][::2]))) #X-Coordinate of 21 subjects
visuals[1].append(list(map(float, row[2:42][::2]))) #Y-Coordinate of 21 subjects
visuals[3].append([float(row[44]),float(row[46])]) #X-Coordinate of the subject I want to display the arrow between
visuals[4].append([float(row[45]),float(row[47])]) #Y-Coordinate of the subject I want to display the arrow between
fig, ax = plt.subplots(figsize = (8,8))
plt.grid(False)
scatter = ax.scatter(visuals[0][0], visuals[1][0], c=['blue'], alpha = 0.7, s = 20, edgecolor = 'black', zorder = 1) #Scatter plot (21 subjects)
scatterO = ax.scatter(visuals[3][0], visuals[4][0], c=['black'], marker = 'o', alpha = 0.7, s = 25, edgecolor = 'black', zorder = 2) #Scatter plot (intended subject)
annotation = ax.annotate('Player 1', xy=(visuals[0][0][0],visuals[1][0][0]), fontsize = 8) #This annotation is displayed at the XY coordinate of the subject in the first column of the dataset
arrow = ax.annotate('', xy = (visuals[3][0][0]), xytext = (visuals[4][0][0]), arrowprops = {'arrowstyle': "<->"}) #This function returns an error
我在做什么不同?
您没有指出发生此错误的位置。但是我想这是最后一行:
arrow = ax.annotate('', xy = (visuals[3][0][0]), xytext = (visuals[4][0][0]), arrowprops = {'arrowstyle': "<->"}) #This function returns an error
从文档中, xy
参数是迭代的,但是在您的代码中, xy
只有1个浮点值,您应该尝试以下操作:
xy=(visuals[3][0][0],visuals[4][0][0]), xytext = (visuals[3][0][1], visuals[4][0][1])
而不是
xy = (visuals[3][0][0])
正如阿玛斯指出的那样,我只有一个浮点值是数据集中第一个主题的XY坐标。我需要添加第二个主题xy坐标。
以下代码有效:
arrow = ax.annotate('', xy = (visuals[3][0][0], visuals[4][0][0]), xytext = (visuals[3][0][1],visuals[4][0][1]), arrowprops = {'arrowstyle': "<->"})