繁体   English   中英

如何将垂直线添加到 plotly (python)

[英]How to add vertical line to plotly (python)

我想在基于日期索引(x 轴)的 dataframe 列“信号”中出现值“信号”时添加一条垂直线
我怎样才能做到这一点?

dataframe:

date                open    high    low     close  signal

2021-06-14 07:04:00 2503.0  2507.4  2502.7  2507.2  -
2021-06-14 07:05:00 2507.2  2509.5  2506.9  2508.1  -
2021-06-14 07:06:00 2508.1  2509.8  2505.5  2506.5  -
2021-06-14 07:07:00 2506.5  2507.2  2503.4  2506.6  -
2021-06-14 07:08:00 2506.6  2506.6  2502.6  2504.8  -
2021-06-14 07:09:00 2504.8  2507.1  2502.5  2503.1  -
2021-06-14 07:10:00 2503.1  2504.0  2501.8  2502.0  -
2021-06-14 07:11:00 2502.0  2502.9  2500.6  2500.6  -
2021-06-14 07:12:00 2500.6  2502.0  2498.9  2500.2  -
2021-06-14 07:13:00 2500.2  2500.8  2497.9  2499.4  -
2021-06-14 07:14:00 2499.4  2500.9  2499.1  2500.2  signal
2021-06-14 07:15:00 2500.2  2501.3  2498.2  2498.3  -
2021-06-14 07:16:00 2498.3  2500.2  2496.1  2499.5  signal
2021-06-14 07:17:00 2499.5  2501.4  2499.1  2500.6  -
2021-06-14 07:18:00 2500.6  2500.6  2497.7  2498.2  -
2021-06-14 07:19:00 2498.2  2501.8  2498.0  2500.9  signal
2021-06-14 07:20:00 2500.9  2500.9  2497.3  2497.4  -
2021-06-14 07:21:00 2497.4  2497.7  2495.2  2495.6  -
2021-06-14 07:22:00 2495.6  2496.3  2494.0  2496.2  -
2021-06-14 07:23:00 2496.2  2497.7  2496.1  2496.3  -
2021-06-14 07:24:00 2496.3  2499.0  2496.0  2498.6  -
2021-06-14 07:25:00 2498.6  2498.6  2496.6  2496.6  -
2021-06-14 07:26:00 2496.6  2498.4  2495.8  2496.5  -
2021-06-14 07:27:00 2496.5  2498.5  2496.4  2497.6  -
2021-06-14 07:28:00 2497.6  2501.6  2497.3  2499.5  signal
2021-06-14 07:29:00 2499.5  2501.4  2499.5  2500.3  -
2021-06-14 07:30:00 2500.3  2504.9  2500.3  2502.8  -
2021-06-14 07:31:00 2502.8  2503.2  2501.8  2501.8  -
2021-06-14 07:32:00 2501.8  2502.2  2500.7  2501.6  -
2021-06-14 07:33:00 2501.6  2501.7  2498.7  2498.9  -
2021-06-14 07:34:00 2499.3  2500.8  2498.2  2500.4  signal
2021-06-14 07:35:00 2500.4  2502.2  2499.6  2500.7  -
2021-06-14 07:36:00 2500.7  2501.5  2498.8  2498.8  -
2021-06-14 07:37:00 2498.8  2499.9  2497.7  2499.2  -
2021-06-14 07:38:00 2499.2  2500.8  2499.2  2499.7  signal
2021-06-14 07:39:00 2499.7  2500.0  2498.2  2499.0  -
2021-06-14 07:40:00 2499.0  2500.0  2497.0  2498.2  -

plotly 到目前为止(没有垂直线):

fig = go.Figure(data=[go.Candlestick(x=df.index,
                open=df.open, 
                     high=df.high,
                     low=df.low,
                     close=df.close)])
fig.show()

如何添加垂直线?

subDf = df[df["signal"] == "signal"]
fig.add_vline(x=subDf.index, ...)

请注意,它从 4.12 版开始可用

在这里查看更多 - https://plotly.com/python/horizontal -vertical-shapes/

通过迭代信号为真的索引并使用fig.add_vline可以添加垂直线。

import pandas as pd
import numpy as np

pd.options.plotting.backend = "plotly"


df = pd.DataFrame(
    {
        "val": np.sin(np.linspace(0, 7, 100))
    }
)
df["signal"] = df.val > 0.7

fig = df.plot(y="val")
for idx in df[df.signal].index:
    fig.add_vline(idx)

创建:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM