How to create a title with multiple lines in Plotly

One of the features of Plotly is that it allows you to customize the appearance of your charts, including the title. We will show you how to create a title with multiple lines in Plotly, using different methods and options.

Method 1: Using HTML tags

One way to create a title with multiple lines in Plotly is to use HTML tags, such as
for line breaks. This method works for both static and interactive charts, and it is supported by the official documentation. Here is an example of how to use this method:

import plotly.graph_objects as go

x = [1, 2, 3]
y = [4, 1, 2]
z = [2, 4, 5]

fig = go.Figure(data=go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=12,
        color=z, 
        colorscale='Viridis',
        opacity=0.8
    )
))

fig.update_layout(
    title=dict(
        text='Pythoneo.com
is my fav
website', font=dict( size=24, color='black' ) ) ) fig.show()

As you can see, the title has three lines, separated by
tags. You can also use other HTML tags, such as b for bolding, i for italicizing, or font for changing the font properties.

See also  Creating Interactive Bar Charts with Plotly in Python

Method 2: Using newline characters

Another way to create a title with multiple lines in Plotly is to use newline characters (\n) in the title text. This method works for static charts only, and it is not recommended by the official documentation. However, it can be useful if you want to avoid using HTML tags or if you want to have more control over the alignment of the title. Here is an example of how to use this method:

import plotly.graph_objects as go

x = [1, 2, 3]
y = [4, 1, 2]
z = [2, 4, 5]

fig = go.Figure(data=go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
marker=dict(
size=12,
color=z,
colorscale='Viridis',
opacity=0.8
)
))

title_text = "Pythoneo.com\nis my fav\nwebsite"
fig.update_layout(
title=title_text,
title_font=dict(
size=24,
color='black'
)
)

fig.show()

scatter plot using Plotly's go.Scatter3d. Instead of using HTML tags, we set the title_text variable to a string with multiple lines separated by newline characters (\n). Then, we update the layout's title with title=title_text, and the result is a title with multiple lines.