Reputation: 1
I have a data file from which columns of data are selected using pandas. I then use the data from these column to plot it on a scatter graph using matplotlib but i get an error which says: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I have searched this problem already on StackOverflow but they don't relate to data plotting
# Getting data from column that user selects
variable1 = location_data.iloc[0:-1, columnPosition1]
variable2 = location_data.iloc[0:-1, columnPosition2]
# Converting data from variable1 and 2 to float and storing in list called x & y
for xValue in variable1:
x.append(float(xValue))
for yValue in variable2:
y.append(float(yValue))
# x and y are lists which hold data from excel file
# Error begins here
plt.scatter(x, y, marker='o')
plt.xlabel(variable1, fontsize = 15)
plt.ylabel(variable2, fontsize = 15)
plt.title('A graph of ' + variable1 + ' and ' + variable2)
Traceback (most recent call last):
File "F:\Computer Science\NEA\Code\Coursework.py", line 115, in <module>
plot_data(variable1, variable2)
File "F:\Computer Science\NEA\Code\Coursework.py", line 96, in plot_data
plt.xlabel(variable1, fontsize = 15)
File "C:\Users\Naima\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\pyplot.py", line 3065, in xlabel
xlabel, fontdict=fontdict, labelpad=labelpad, **kwargs)
File "C:\Users\Naima\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\axes\_axes.py", line 265, in set_xlabel
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
File "C:\Users\Naima\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\axis.py", line 1564, in set_label_text
self.label.set_text(label)
File "C:\Users\Naima\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\text.py", line 1191, in set_text
if s != self._text:
File "C:\Users\Naima\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Upvotes: 0
Views: 1697
Reputation: 50116
You are setting your data series as the label string:
plt.xlabel(variable1, fontsize = 15)
Since plt.xlabel
and plt.ylabel
do not check the type of the label, they chokes when trying to treat your series as a string.
Set an explicit string as the label instead:
plt.xlabel("The X Axis", fontsize = 15)
Upvotes: 0