Charles Fox
Charles Fox

Reputation: 281

How can a plotly legend entry be put on two lines (\n in legend text)?

I would like to split my legend text into two lines. This would help increase the width of the plot and reduce the width of the legend. However, when I use \n, it gets replaced with a space. Is there a way to split the text into two lines?

import plotly.graph_objs as go
from plotly.offline import plot
import numpy as np

x = np.arange(0, 100)
y = np.random.normal(size=100)

fig = go.Figure()

fig.add_trace(go.Scatter(
        name='A very long\nlegend entry',
        x=x,
        y=y,
        showlegend=True,
        mode='lines',
        line={'color': 'Black'}))

fig.update_layout(
        legend={
            'x': 1.01,
            'y': 0.5})

plot(fig)

\n in legend text

Upvotes: 4

Views: 8839

Answers (1)

nicolaskruchten
nicolaskruchten

Reputation: 27430

Trace names are interpreted as pseudo-HTML so you can use <br> instead of \n for newlines: name='A very long<br>legend entry'.

Upvotes: 14

Related Questions