pythonplotlyvisualization

Plotly: Increasing figure size to make room for long footnote


Building on the example in this thread, I have the following code:

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

fig = go.Figure(go.Scatter(x=df['Date'], y=df['AAPL.High']))

fig.update_xaxes(rangeslider_visible=True)

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><br>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><br>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><br>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=10), 
    xref='paper',
    x=0,
    yref='paper',
    y=-1.5
    )

fig.show()

The annotion is too long to fit in the figure, so it gets cut off. Is there a way to add space at the bottom of the figure such that the annotation fits?


Solution

  • You can do it this way:

    import plotly.graph_objects as go
    import pandas as pd
    
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
    
    fig = go.Figure(go.Scatter(x=df['Date'], y=df['AAPL.High']))
    
    fig.update_xaxes(rangeslider_visible=True)
    
    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><br>'
      )
    
    fig.add_annotation(
        showarrow=False,
        text=note,
        font=dict(size=10), 
        xref='paper',
        x=0,
        yref='paper',
        y=-1.5
    )
    
    fig.update_layout(
        margin=dict(l=50, r=50, t=50, b=200) 
    )
    
    fig.show()
    
    

    which gives

    [![enter image description here][1]][1]

    You can of course adjust size and position to your liking. [1]: https://i.sstatic.net/161p2n3L.png