From other questions about this (in R though) I understood that you should be able to use HTML in (some?) text elements in Plotly. However, if I try this to get a new line in a text I add to my plot it will simply display the html tag as text and not 'parse' it. I also tried adding '\n' but that just gets ignored.
The code for the trace I'm using;
trace = go.Scattergl(
x=[0.5],
y=[4.5],
text=['A: {} <br> B: {}\nC: {}\nD: {}'.format(a, b, c, d)],
mode='text'
)
Other than that I'm using the 'standard' code to generate the graph;
traces = [trace]
layout = {
'xaxis':{
'range':[0,7],
'showgrid': False,
},
'yaxis':{
'range':[0,7],
'showgrid': False,
},
}
fig = dict(data=traces, layout=layout)
plot(fig)
How can I add a new line in this situation?
I found that using <br>
works inside hovertext, but not in the text placed directly on the figure. That was good enough for my use case, maybe it will help you too? Here's an example similar to what you provided in your question that demonstrates the difference. The string used in the hover text and the annotation is the same, but the break is only registered in the hovertext.
import plotly.graph_objects as go
a = 1
b = 2
c = 3
d = 4
trace = go.Scattergl(
x=[0.5],
y=[4.5],
text=f'A: {a} <br> B: {b}\nC: {c}\nD: {d}',
hoverinfo='text',
mode='markers+text'
)
traces = [trace]
layout = {
'xaxis':{
'range':[0,7],
'showgrid': False,
},
'yaxis':{
'range':[0,7],
'showgrid': False,
},
}
fig = go.Figure(data=traces, layout=layout)
fig.show(renderer='browser')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With