【问题标题】:Matplotlib ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()Matplotlib ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
【发布时间】:2020-01-28 07:55:04
【问题描述】:

当我使用下面的代码制作一个绘图时,我将自定义值分配给 x 轴并设置xlim,它成功了。

x = np.array([0,1,2,3])
y = np.array([0.650, 0.660, 0.675, 0.685])
customized_x = np.array([3, 4, 5, 6])
plt.xticks(x, customized_x)
plt.xlim(2, 3)
plt.plot(x, y)
plt.show()

但是,因为我想一起制作更多的情节,所以当我改用 subplot 时,它会在ax1.set_xticks(x, customized_x) 行弹出:

fig = plt.figure(figsize=[6.0, 9.0])
ax1 = fig.add_subplot(111)

x = np.array([0,1,2,3])
y = np.array([0.650, 0.660, 0.675, 0.685])
customized_x = np.array([3, 4, 5, 6])
ax1.set_xticks(x, customized_x)
ax1.set_xlim(2, 3)
ax1.plot(x, y)
plt.show()

得到的错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

虽然 我可以通过不使用子图来避免这个问题,我仍然想知道子图有什么问题?

编辑

我找到了solution to the similar question here。抱歉重复了。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您似乎错误地使用了set_xticks。来自matplotlib 文档:

    Axes.set_xticks(self, ticks, minor=False)¶

    这是因为 Axes.set_xticksplt.xticks 相比具有不同的参数。代码尝试将customized_x 评估为minor 参数。

    我认为你需要的是Axes.set_xticklabels:见the documentation here

    【讨论】:

      猜你喜欢
      • 2020-05-12
      • 2016-01-26
      • 2019-07-30
      • 2014-04-06
      相关资源
      最近更新 更多