Search code examples
pythonplotly

Annotation not found outside plotly graph


I have a graph that looks like this:

where I want to add some text towards the left bottom side of the plot, something similar to the text at the bottom here, but for me on my left or right side of the graph.

enter image description here

I searched on stack and found many solutions, even one specific to the graph shown,however none work for me. My current code is below, where the annotation does not display on my plot.

data1= final_api.query("info_title=='JupyterHub'").sort_values(by=['commitDate'])
data1['Year-Month'] = pd.to_datetime(data1['Year-Month']) 
data1['Commit-growth'] = data1['commits'].cumsum()
import plotly.graph_objects as go
fig = go.Figure()


fig = px.scatter(data1, x='Year-Month', y='Commit-growth', color='major_version', text='Commit-growth')

fig.add_trace(go.Scatter(mode='lines',
                         x=data1["Year-Month"],
                         y=data1["Commit-growth"],
                         line_color='black',
                         line_width=1,
                         line_shape='hvh',
                         showlegend=False
                       )
             )

for _,row in data1.iterrows():
    fig.add_annotation(
        go.layout.Annotation(
            x=row["Year-Month"],
            y=row["Commit-growth"],
            text=row['info_version'],
            showarrow=False,
            align='center',
            yanchor='bottom',
            yshift=5,
            textangle=-10)
    )


note = 'NYSE Trading Days After Announcement<br>Source:<a href="https://www.nytimes.com/"">The NY TIMES</a> Data: <a href="https://www.yahoofinance.com/">Yahoo! Finance</a>'
fig.add_annotation(
    showarrow=False,
    text=note,
    font=dict(size=5), 
    xref='x domain',
    x=0.5,
    yref='y domain',
    y=-0.5
    )

fig.update_layout(template='plotly_white',title_text=' Version Change in Jupyter Hub API by commits',title_x=0.5,
                  xaxis_title='Year-Month', yaxis_title='Number of Commits', yaxis_range=[0, 400],height=760, width=1600, xaxis_range=['2016-06-01', '2021-04-01'])
fig.update_traces(textposition="bottom right", showlegend=False,marker_size=10,marker_line_width=2, marker_line_color='black')

fig.show()

Any help on this would be really helpful.


Solution

  • Edit: figured how to do it by myself

    Add another annotation like this, although it gives the text at the upper left corner of the graph,works for me. Just add this code instead of the previous annotation code in the question,the rest of the code remains the same.These dimensions work for the particular alignment mentioned.

    fig.add_annotation(
        showarrow=False,
        text="23 paths over 450 updates",
        font=dict(size=15),
        xref='paper',
        x=0.014,
        yref='paper',
        y=1.077
        )