如何在 plotly 中为图表创建带有换行符的标题

How to create a title with a newline for a chart in plotly

我需要用换行符在 plotly 上创建一个图。最好上面的文字比第二行大,但不是必须的。

fig.update_layout(
    title=go.layout.Title(
        text=title,
        xref="paper",
        x=0.5,
    ),

这个标题我试过了

title = "Hello \n World"
title = "Hello" + "\n" + "World"
title = "$Hello \ World$"
title = "$Hello \newline World$"

全部运行没有错误,但换行被忽略

我不确定如何实现它。谢谢你的帮助。

您可以在标题中添加一个 <br> 标签,它将被解释为一个 HTML 换行符。 this doc 支持这一点。您可以在以下示例中看到结果:

from dash import Dash
import dash_core_components as dcc
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5],
                    'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data <br> Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)