【问题标题】:How to I solve a ValueError: the truth value如何解决 ValueError:真值
【发布时间】:2019-09-20 22:11:06
【问题描述】:

我有一个数据文件,使用 pandas 从中选择数据列。然后,我使用这些列中的数据使用 matplotlib 将其绘制在散点图上,但我收到一条错误消息:ValueError:系列的真值不明确。使用 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().

【问题讨论】:

  • 这是一个常见错误。您能否将实际的错误消息以及部分或全部数据框复制并粘贴到您的问题中?
  • Rahim,如何制作minimal reproducible example 是一本很好的读物,它将帮助您获得所需的答案。
  • 您确定错误确实发生在代码的那部分吗?
  • @run-out 我已经按照您的建议进行了修改。您需要更多代码或信息吗?
  • @MisterMiyagi 我已经粘贴了错误消息并评论了错误开始的位置。你还需要什么吗?

标签: python pandas matplotlib python-3.7


【解决方案1】:

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

plt.xlabel(variable1, fontsize = 15)

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 2018-07-22
    • 1970-01-01
    相关资源
    最近更新 更多