问题描述
在我的桌面上使用Sppyder运行此代码可以正确显示标记。在笔记本电脑上使用相同的代码,不再显示它们。
我有相同的spyder(python 3.7)版本。如果我颠倒了代码行,然后在可以看到它们的行之前放置了市场,但是该行越过了标记。我想要的是标记在线上。
fig = plt.figure()
ax1 = fig.add_subplot(111,ylabel='Price in $')
df_MA.iloc[:,0].plot(ax=ax1,color='k',lw=1.)
df_MA[['short_MA','long_MA']].plot(ax=ax1,lw=2.)
ax1.plot(df_MA.loc[df_MA.positions == 1.0].index,df_MA.short_MA[df_MA.positions == 1.0],'^',markersize=10,color='g')
ax1.plot(df_MA.loc[df_MA.positions == -1.0].index,df_MA.short_MA[df_MA.positions == -1.0],'v',color='r')
plt.show()
解决方法
使用功能Zorder
,然后将我的标记代码行放入第一行。
fig = plt.figure()
ax1 = fig.add_subplot(111,ylabel='Price in $')
ax1.plot(df_MA.loc[df_MA.positions == 1.0].index,df_MA.short_MA[df_MA.positions == 1.0],'^',markersize=10,color='g',zorder=2)
ax1.plot(df_MA.loc[df_MA.positions == -1.0].index,df_MA.short_MA[df_MA.positions == -1.0],'v',color='r',zorder=2)
df_MA.iloc[:,0].plot(ax=ax1,color='k',lw=1.,zorder=0)
df_MA[['short_MA','long_MA']].plot(ax=ax1,lw=2.,zorder=1)
plt.show()