python - ValueError : The truth value of array with more than one element is ambiguous. 使用 a.any() 或 a.all()

标签 python loops numpy

我正在尝试迭代 Numpy 值,但似乎出现错误。

for ax in [ax1, ax2, ax3]:
   ax.axvline(enter_pos, c ='g')
   ax.axvline(exit_pos, c = 'r')

但我收到此错误:

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

我知道 Stackover flow 上还有其他关于此问题的答案,但我不知道该怎么办。答案没有强调如何更改代码以适应 for 循环,这似乎让我绊倒了。

我已经尝试过:

ax in [ax1], ax in [ax2], ax in [ax3]

ax[ax1 & ax2 & ax3]

但是还没有任何效果。

想法?

* 编辑 *

这里是更多代码:

    ax1 = plt.subplot(311)
    data[[ticker[0], ticker[1]]].plot(ax = ax1)
    plt.ylabel('Price')
    plt.setp(ax1.get_xticklabels(), visible=False)

    ax2 = plt.subplot(312, sharex=ax1)
    results.spread.plot(ax=ax2, color='k')
    ax2.axhline(2, color='k')
    ax2.axhline(5, color='k')
    plt.ylabel('Spread')
    plt.setp(ax2.get_xticklabels(), visible=False)

    ax3 = plt.subplot(313, sharex=ax1)
    results.portfolio_value.plot(ax=ax3, color='k')
    plt.ylabel('Portfolio Value')

    # Plot spread enter and exit markers
    enter_pos = results.index[results.buy_spread]
    exit_pos = results.index[results.sell_spread]

    for ax in [ax1, ax2, ax3]:
        ax.axvline(enter_pos, c ='g')
        ax.axvline(exit_pos, c = 'r')

    plt.gcf().set_size_inches(16, 12)

* 编辑 2 *

我想说关于第二个循环的注释是正确的,但我仍然遇到与此代码相同的错误:

for ax in [ax1, ax2, ax3]:
  for pos in enter_pos:
    ax.axvline(enter_pos, c ='g')
    ax.axvline(exit_pos, c = 'r')

最佳答案

axvline 仅支持一个数字,您需要第二个循环:

for ax in [ax1, ax2, ax3]:
    for pos in enter_pos:
        ax.axvline(pos, c ='g')
    for pos in exit_pos:
        ax.axvline(pos, c ='r')

但是如果enter_pos的大小很大,它可能会很慢。您可以使用 LineCollection 相反,这里是一个示例:

import pylab as pl
import numpy as np
from matplotlib import collections
from matplotlib import transforms

def axvlines(ax, x, **kw):
    from matplotlib import collections
    from matplotlib import transforms

    x = np.asanyarray(x)
    y0 = np.zeros_like(x)
    y1 = np.ones_like(x)        
    data = np.c_[x, y0, x, y1].reshape(-1, 2, 2)
    trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
    lines = collections.LineCollection(data, transform=trans, **kw)
    ax.add_collection(lines)

您可以将其用作:

axvlines(enter_pos, colors="g")
axvlines(exit_pos, colors="r")

通过使用 axvlines(),您甚至可以为线条创建颜色图:

X = np.logspace(-1, 0, 50)
fig, ax = pl.subplots()
axvlines(ax, X, cmap="jet", array=np.linspace(0, 1, 50))

这是输出:

enter image description here

关于python - ValueError : The truth value of array with more than one element is ambiguous. 使用 a.any() 或 a.all(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28534198/

相关文章:

python - 有没有更好的方法来通过 HTTP 提供昂贵的阻塞 python 进程的结果?

python - 理解和优化 pyCUDA 中的线程、 block 和网格

c++ - 为什么我们不能在 for 循环中声明两个变量?

python - 在 numpy 数组中传播最大值

python - numpy 矩阵乘法每列和总和

python - numpy 中列表列表的平均值

python - 如何解决 python dropbox 上传脚本中的超时错误?

python - 我可以指定在 .coveragerc 中运行哪些测试文件吗?

C++执行时间测试

php MySQL在嵌套div中循环