如何解决ValueError:真值

时间:2019-05-02 11:35:33

标签: python pandas matplotlib python-3.7

我有一个数据文件,可使用熊猫从中选择数据列。然后,我使用这些列中的数据使用matplotlib将其绘制在散点图上,但出现错误,提示:ValueError:Series的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。

我已经在StackOverflow上搜索了此问题,但是它们与数据绘制无关

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

1 个答案:

答案 0 :(得分:1)

您正在将数据系列设置为标签 string

plt.xlabel(variable1, fontsize = 15)

由于plt.xlabelplt.ylabel不检查标签的类型,因此在尝试将您的系列视为字符串时会阻塞。

设置显式字符串作为标签:

plt.xlabel("The X Axis", fontsize = 15)