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

冷暖自知 提交于 2020-03-23 23:52:48

问题


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!


回答1:


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:

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.


来源:https://stackoverflow.com/questions/58093880/plotly-how-to-format-text-underline-bold-italic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!