LaTeX-text does not work at all in plotly.offline?

I tried the following code from the plotly homepage and only changed one word …

import plotly.plotly as py - > new-> import plotly.offline as py

import plotly.offline as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[1, 4, 9, 16],
    name='$\\alpha_{1c} = 352 \\pm 11 \\text{ km s}^{-1}$'
)
trace2 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[0.5, 2, 4.5, 8],
    name='$\\beta_{1c} = 25 \\pm 11 \\text{ km s}^{-1}$'
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        title='$\\sqrt{(n_\\text{c}(t|{T_\\text{early}}))}$'
    ),
    yaxis=dict(
        title='$d, r \\text{ (solar radius)}$'
    )
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='latex')

I got this graph:

Why isn’t the LaTeX code interpreted? Do I somthing wronge?

Thanks,
Peter

Hi Peter,

You’re not doing anything wrong, this is a know issue (https://github.com/plotly/plotly.py/issues/515) that I’m actively working on (https://github.com/plotly/plotly.py/issues/515 and https://github.com/plotly/plotly.js/pull/2994). The plan is to release support for this in plotly.py 3.4.0, hopefully in about a month.

-Jon

Jon, thanks for the answer. I was using the search before, but could not find what I was looking for.

Peter

Hello,
I saw this thread on stackoverflow:

I am using mac os x 10.14, python 3.7 and plotly 3.4.2 .

Unfortunately plotly.offline.iplot doesn’t display latex text on the chrome browser:

The worst is that, even if the web version shows me the latex text, it does not save it in png (I see in this case J_1)! I cannot exploit my graphs! I am reduced to produce the figure in plotly and add the latex inscription by hand using illustrator…

Thanks for the attention!

Hi @rtournem,

What is your browser version? Here’s what I see with this simple example on Chrome Version 70.0.3538.102 (I’m also on OS X). Could you try out this exact example?

from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go
init_notebook_mode()

fig = go.Figure(layout={'title': '$J_1$'})
iplot(fig)

The LaTeX title is also preserved when I click the modebar to export as an image, or when I export using orca with plotly.io.write_image.

Are you working in the classic Jupyter notebook or in JupyterLab? Also, does LaTeX work for you inside a markdown cell? e.g. put

variable $J_1$

in a Jupyter Notebook markdown cell and see if this renders as LaTeX.

-Jon

Hi Jon,

Encouraged through your answer, I tried your example code. It works fine in my Jupiter notebook too. In addition, I’m able to export this graph to plot.ly and get an perfect graph in the browser too.

Then I tried my code from the first post:

import plotly.offline as py
import plotly.graph_objs as go
import plotly.io as pio

trace1 = go.Scatter(
   x=[1, 2, 3, 4],
   y=[1, 4, 9, 16],
   name='$\\alpha_{1c} = 352 \\pm 11 \\text{ km s}^{-1}$'
)
trace2 = go.Scatter(
   x=[1, 2, 3, 4],
   y=[0.5, 2, 4.5, 8],
   name='$\\beta_{1c} = 25 \\pm 11 \\text{ km s}^{-1}$'
)
data = [trace1, trace2]
layout = go.Layout(
   xaxis=dict(
       title='$\\sqrt{(n_\\text{c}(t|{T_\\text{early}}))}$'
   ),
   yaxis=dict(
       title='$d, r \\text{ (solar radius)}$'
   )
)
fig = go.Figure(data=data, layout=layout)
#pio.write_image(fig, 'pic_23.png', width = 1280, height = 1024)
plot_url = py.plot(fig, filename='latex')

If I run this code as shown, I get an graph opened up in my browser, which isn’t right. It doesn’t matter, whether I sue IE or Chrome. It looks like:

If I uncommand the second to last command line, orca writes an perfect image to the disk!


It seems to me, that the plot command from plotly.offline does not work we would expect.

Thanks again for your work!

Peter

Hi @LPG,

When outputting a plot to an HTML file, you need to explicitly enable MathJax in the output. Replace your last line with

plot_url = py.plot(fig, filename='latex', include_mathjax='cdn')

Hope that helps!
-Jon

Now it’s perfect! It works! Now can I do what I would like to do, wonderful graphs.

Thanks a lot!

Peter

1 Like