Search code examples
pythonpandasplotly

Add Markers in Viz for particular values for column in plotly


I am trying to add marker's in scatter plot if [df['signals1'] == -1] in ploltly.But the markers are not getting added.

df['signals1'].head(10)

Date        Signals
2021-06-04   -1
2021-06-07   -1
2021-06-08   -1
2021-06-09   -1
2021-06-10   -1
2021-06-11   -1
2021-06-14   -1
2021-06-15   -1
2021-06-16   -1
2021-06-17   -1

Current Viz- enter image description here

Code-

fig = go.Figure(data = [ 
              go.Scatter(x=df.index, y=[df['signals1'] == -1], mode = 'markers',
              marker =dict(symbol='triangle-down', size = 16),
              name='Flag')])

In matplotlib, below code is working fine.

u1, = bx.plot(df['Close'][df['signals1'] == -1], lw=0, marker='^', markersize=8, c='g',alpha=0.7)

Ref link -https://medium.com/analytics-vidhya/statistical-arbitrage-with-pairs-trading-and-backtesting-ec657b25a368


Solution

  • The reason why the signal values could not be drawn is that the column names in the data and the column names used in the graph differ in case. Besides, the signal value is a negative value, so the current y-axis is a price, so a price must be specified. So I am modifying the data presented to fit the code.

    import plotly.express as px
    import plotly.graph_objects as go
    import pandas as pd
    import io
    
    data = '''
    Date        Signals
    2015-03-04   133
    2015-04-07   125
    2015-05-08   145
    2016-03-04   133
    2016-04-07   125
    2016-05-08   145
    2017-03-04   133
    2017-04-07   125
    2017-05-08   145
    '''
    
    df_s = pd.read_csv(io.StringIO(data), delim_whitespace=True)
    df_s['Date'] = pd.to_datetime(df_s['Date'])
    df_s.set_index('Date', inplace=True)
    
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
    
    fig = px.line(df, x='Date', y='AAPL.High', title='Time Series with Range Slider and Selectors')
    
    fig.add_trace(go.Scatter(
        x=df_s.index,
        y=df_s['Signals'],
        mode = 'markers',
        marker =dict(symbol='triangle-down', size = 16),
        name='Flag'
    )
                 )
    
    fig.update_xaxes(
        rangeslider_visible=True,
        rangeselector=dict(
            buttons=list([
                dict(count=1, label="1m", step="month", stepmode="backward"),
                dict(count=6, label="6m", step="month", stepmode="backward"),
                dict(count=1, label="YTD", step="year", stepmode="todate"),
                dict(count=1, label="1y", step="year", stepmode="backward"),
                dict(step="all")
            ])
        )
    )
    # update
    fig.update_layout(template='plotly_dark',
                      xaxis_rangeselector_font_color='black',
                      xaxis_rangeselector_activecolor='red',
                      xaxis_rangeselector_bgcolor='green',
                     )
    fig.show()
    

    enter image description here