我正在尝试注释从列表列表的 XY 坐标派生的箭头。我可以让绘图在某个点显示注释,但当我尝试在两个 XY 坐标之间添加箭头时,出现类型错误:'float'对象不可迭代
。
代码如下:
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
来自 doc , xy
参数是可迭代的,但在您的代码中,xy
只有 1 个浮点值,您应该尝试如下操作:
xy=(视觉效果[3][0][0],视觉效果[4][0][0]), xytext = (视觉效果[3][0][1],视觉效果[4] [0][1])
而不是
xy = (视觉效果[3][0][0])
关于python - 类型错误 : 'float' object is not iterable trying to annotate an arrow from a list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48676568/