ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.

今天遇到一个神奇的错误,苦寻百度半天无果,代码如下。
def drawing_plot(df_obj):
index = df_obj.iloc[:, 0]
ax = plt.figure()
plt.xticks(np.arange(len(index)), df_obj.index, rotation=45) # 修改坐标轴刻度,使得柱状图可以按顺序排列显示
bar_width = 0.4
x = np.arange(len(index))
y_1 = df_obj.iloc[:, 1]
y_0 = df_obj.iloc[:, 0]
plt.text(x, y_1, y_1)
plt.bar(np.arange(len(index)) - bar_width / 2, df_obj.iloc[:, 1], bar_width, color=‘g’)
报错如下
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
确定是plt.text()参数出错,搜索提示函数会把参数都转成浮点数,因为参数对象不对无法转换,随后换成列表对象任然报错,别人用列表就不报错,很神奇。最后没法办尝试遍历过程中添加文本就正常了,绝对是个大坑,代码如下:
def drawing_plot(df_obj):
index = df_obj.iloc[:, 0]
ax = plt.figure()
plt.xticks(np.arange(len(index)), df_obj.index, rotation=45) # 修改坐标轴刻度,使得柱状图可以按顺序排列显示
bar_width = 0.4
x = np.arange(len(index))
y_1 = df_obj.iloc[:, 1]
y_0 = df_obj.iloc[:, 0]
fig, axes = plt.subplot(2)
plt.bar(np.arange(len(index)) - bar_width / 2, df_obj.iloc[:, 1], bar_width, color=‘g’)
for x1, y1 in zip(x, y_1):
plt.text(x1-bar_width/2, y1, y1)
plt.bar(np.arange(len(index))+bar_width/2, df_obj.iloc[:, 0], bar_width, color=‘r’)
for x0, y0 in zip(x, y_0):
plt.text(x0+bar_width/2, y0, y0)
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值