How to add a text area with custom text as a kind of legend in a plotly plot

Is it possible to add a custom text area (like a legend but I should be able to add my own text) in a python plotly plot ?

Hi @ULTRA,

Yes, you can do this with a text annotation in paper coordinates. There is an example at https://plot.ly/python/text-and-annotations/#adding-annotations-with-xref-and-yref-as-paper where annotations are used as replacements for axis labels.

Also, if you need to have text spread across multiple lines you can add line breaks by inserting <br> sequences.

Hope that helps!

-Jon

Hello @jmmease,

I am a bit confused at the explanation you have given :sweat_smile:
Basically, I was looking for something similar across the lines of this image :
Capture
Here there are 2 text boxes besides the plot with some custom information. Is there a way to achieve something similar to this ?

Hi @ULTRA,

Here’s a full example of what I was talking about:

import plotly.io as pio
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

fig = go.Figure(
    data=[
        go.Scatter(y=[2, 1, 3])
    ],
    layout=go.Layout(
        annotations=[
            go.layout.Annotation(
                text='Some<br>multi-line<br>text',
                align='left',
                showarrow=False,
                xref='paper',
                yref='paper',
                x=1.1,
                y=0.8,
                bordercolor='black',
                borderwidth=1
            )
        ]
    )
)
iplot(fig)

Hope that helps,
-Jon

1 Like

Is it possible to embed more complex html in the text attribute ?

Hi @ULTRA,

Here is the subset of HTML that is supported: Links, Text, and HTML in Graphs.

-Jon

1 Like

@jmmease it looks like color isn’t supported?

Also, what is the role of the x, y coordinates? Based on your example above, they don’t appear to determine the location of the box.

Upon further exploration, it appears that they represent fractions of the plot e.g. if x=0.9, the x-coordinate of the box will start at the 90% of the width of the plot.