Reputation: 214
I am trying to annotate an arrow derived from the XY coordinates of a list of lists. I can get the plot to display an annotation at a certain point but get a Type Error:'float' object is not iterable
when I try to add an arrow between two XY coordinates.
The code is shown below:
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
What am I doing differently?
Upvotes: 1
Views: 777
Reputation: 1080
You didn't point out where this error occurred. But I guess it's in the last line:
arrow = ax.annotate('', xy = (visuals[3][0][0]), xytext = (visuals[4][0][0]), arrowprops = {'arrowstyle': "<->"}) #This function returns an error
from the doc, xy
parameter is iterable, but in your code, the xy
have only 1 float value, you should try something like:
xy=(visuals[3][0][0],visuals[4][0][0]), xytext = (visuals[3][0][1], visuals[4][0][1])
instead of
xy = (visuals[3][0][0])
Upvotes: 2
Reputation: 214
As Amarth pointed out I only had one float value being the xy coordinate of the first subject in the dataset. I needed to add the second subjects xy coordinate.
The following code works:
arrow = ax.annotate('', xy = (visuals[3][0][0], visuals[4][0][0]), xytext = (visuals[3][0][1],visuals[4][0][1]), arrowprops = {'arrowstyle': "<->"})
Upvotes: 1