Plotly: How to format text (underline, bold, italic)

Question:

I try to underline text in plotly when using annotations.
I add my annotations using

import plotly.graph_objects as go
g = go.FigureWidget(make_subplots(rows=1,cols=1))
g.update_layout(annotations=[dict(text='my text')]) #plus any other parameters

Is there an option (in the annotations dict, maybe?) to have underlined text?

Thanks!

Asked By: MaxS

||

Answers:

Plotly uses a subset of HTML tags to format text like bold '<b></b>' and italics '<i></i>'. Alas, '<u></u>' does not seem to be included at the moment. But linebreak is included, so you could make a little work-around like this:

string = "These are orange"
myText = string+'<br>'+ '-'*len(string)

Plot:

enter image description here

Code:

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])

string = "These are orange"
myText = string+'<br>'+ '-'*len(string)

fig.update_layout(annotations=[dict(x='orangutans',y = 15, text=myText, font=dict(family='Courier New, monospace'))])
fig.show()

Plotly source: using help(fig.layout):

  text
 |                  Sets the text associated with this annotation.
 |                  Plotly uses a subset of HTML tags to do things
 |                  like newline (<br>), bold (<b></b>), italics
 |                  (<i></i>), hyperlinks (<a href='...'></a>).
 |                  Tags <em>, <sup>, <sub> <span> are also
 |                  supported.
Answered By: vestland

Came here to find an answer, and found a better one from the reply.
Since "span" is supported. you can do

string = "These are orange"
myText = "<span style='text-decoration:underline;'>"+string+"</span>"
Answered By: O'Young