Add annotation spanning multiple lines

I have the following piece of code:

import plotly.graph_objects as go

fig = go.Figure()
fig.update_layout(
    annotations=[go.layout.Annotation(xref='paper',
                                      yref='paper',
                                      x=1,
                                      xanchor= 'right',
                                      y=1,
                                      yanchor= 'bottom',
                                      showarrow=False,
                                      text='Acceptance: '+str(11)+'%\nAlpha:'),]
)
fig.show()

Which produces the following plot:

The percentage will be specified by a variable. Also, I want to specify the value of Alpha in a new line.

Why doesn’t \n push Alpha to the next line?

The annotation string is passed directly as HTML so you can use a <br> element to break lines. For string formatting, you can use any kind of Python string formatting such as f strings for Python 3 text=f'Acceptance {var}' where var is your variable name.

3 Likes