LaTeX in layout title does not work properly

import plotly as py
import plotly.graph_objs as go

py.offline.init_notebook_mode(connected=True)
layout = go.Layout(title="This is a test: $2$")
data = [go.Scatter(x=[1, 2, 3], y=[4, 5, 6])]
fig = go.Figure(data=data, layout=layout)
py.offline.iplot(fig)

As you can see the title is not rendered properly (only the math portion is displayed). Version is 3.7.1.

Hi @bx6YYXE6,

At present you place LaTeX expressions inside the standard HTML-style text string. You’ll need to make the entire title string a single LaTeX expression. For example…

layout = go.Layout(title=r"$\text{This is a test:} \sqrt{2^4}$")

Hope that helps!
-Jon

1 Like

Thanks. I’ll accept that as a solution although this makes the figure a bit ugly imho since this typesetting of the text portion will create a aesthetical discrepancy between the title and the rest of the figure. But I guess it is the only way to get some math rendered there.

It’s also worth mentioning that you can get pretty far using only the HTML markup and HTML symbol codes. This will cover, for example, super/sub scripts and greek letters.

import plotly as py
import plotly.graph_objs as go

py.offline.init_notebook_mode(connected=True)
layout = go.Layout(title="This is a test: &#960;r<sup>2</sup>")
data = [go.Scatter(x=[1, 2, 3], y=[4, 5, 6])]
fig = go.Figure(data=data, layout=layout)
py.offline.iplot(fig)

-Jon

1 Like

@jmmease Are there any plans to add sometime in the future the LaTeX title feature to axis titles for 3d graphs?

It works great for the plot title, but the dollar signs seem to be ignored as special characters when using them as the values of “xaxis_title”, “yaxis_title”, “zaxis_title” in the “scene” dictionary of Layout.

1 Like

Can you please be so kind and tell me why fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$") does not work in the following code

import plotly.express as px
fig = px.bar(df, x=["Apples", "Oranges"], y=[10,20], color=["Here", "There"],
    labels=dict(x="Fruit", y="Amount", color="Place")
)

fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$", font_size=16)
                )

fig.show()

gives

Edit: This appears to be a browser issue. The above graph was created using Firefox.

Here is the graph when run in a Jupyter Notebook in Safari with y-axis title.

Note, that although now visible text="$\text{This is a test:} \sqrt{2^4}$" is still not displayed correctly. Any suggestions on this?

Edit 2: Forgot the r; with text=r"$\text{This is a test:} \sqrt{2^4}$" it is actually correctly displayed in Safari