'How to add vertical line to plotly (python)

I want to add a vertical line when value 'signal' occurs in dataframe column 'signal' based on date index (x-axis)
How can I achieve this?

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 so far (without the vertical line):

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

How do I add the vertical line?



Solution 1:[1]

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

Note that it is available from version 4.12

See more here - https://plotly.com/python/horizontal-vertical-shapes/

Solution 2:[2]

By iterating the indices where the signal is true and using fig.add_vline the vertical lines can be added.

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)

Creates:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dror