首页 文章

在特定点添加标记到绘图

提问于
浏览
0

我有一个数据帧(前几行):

我可以用 matplotlib.pyplot 绘制它:

fig = plt.figure()
ax1 = fig.add_subplot(111,  ylabel='Price')

df1[['Close']].plot(ax=ax1)

要得到:

我想要做的是在索引 2018-09-10 04:00:00 处添加一个标记到绘图,向下三角形,这由数据框的位置列中的值 -1 指示 .

我试着这样做:

fig = plt.figure()
ax1 = fig.add_subplot(111,  ylabel='Price')

df1[['Close']].plot(ax=ax1)
ax1.plot(
    df1.loc[df1.positions == -1.0].index,
    df1.Close[df1.positions == -1.0],
    'v', markersize=5, color='k'
)

我得到这样的情节:

所以有两件事 . 一个是指数转换为射击到2055年的东西,我不明白为什么 . 还有一种方法只使用第一个 plot 呼叫在特定位置添加标记吗?我尝试使用 markevery 但没有成功 .

1 回答

  • 2

    如果要组合pandas图和matplotlib日期时间图,需要在兼容模式下绘制pandas图

    df1['Close'].plot(ax=ax1, x_compat=True)
    

    这可能已经给你想要的情节了 .

    如果您不想使用matplotlib,则可以绘制已过滤的数据帧

    df1['Close'].plot(ax=ax1)
    df1['Close'][df1.positions == -1.0].plot(ax=ax1, marker="v", markersize=5, color='k')
    

相关问题