我试图迭代Numpy值,但我似乎得到了一个错误。在
for ax in [ax1, ax2, ax3]:
ax.axvline(enter_pos, c ='g')
ax.axvline(exit_pos, c = 'r')
但我得到一个错误:
^{pr2}$我知道这个问题还有其他的答案,但我不知道该怎么做。答案并没有突出说明如何更改代码以适应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
只支持一个数字,您需要第二个循环:但是如果
^{pr2}$enter_pos
的大小很大,它可能很慢。您可以使用LineCollection
istead,下面是一个示例:您可以将其用作:
通过使用
axvlines()
,您甚至可以为这些线创建一个颜色映射:输出如下:
相关问题 更多 >
编程相关推荐