今天遇到一个神奇的错误,苦寻百度半天无果,代码如下。
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()
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.
最新推荐文章于 2024-06-21 15:47:41 发布