I am trying to get Latex symbols in titles and labels of a Plotly figure. I am using VSCode and I run the code in Interactive Window. Latex usage looks really simple in Jupyter Notebook, from what I saw in other posts, but I can't get it to work within this environment.
My env:
python 3.10.4
plotly 5.9.0
vscode 1.62.3
What I tried:
This basic code snippet should work according to most posts I have seen but does not do the Latexrendering in the Interactive Window. It has been taken from https://plotly.com/python/LaTeX/, where everything looks so easy. That's why I am guessing the issue is related to VSCode.
import plotly.graph_objs as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4],
y=[1, 4, 9, 16],
name=r'$\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}$'
))
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4],
y=[0.5, 2, 4.5, 8],
name=r'$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$'
))
fig.update_layout(
xaxis_title=r'$\sqrt{(n_\text{c}(t|{T_\text{early}}))}$',
yaxis_title=r'$d, r \text{ (solar radius)}$'
)
fig.show()
This is a known issue with Plotly in Jupyter notebooks in VSCode (e.g. issues #7801 and #8131).
Tomas Mazak shared a workaround in #8131:
import plotly
import plotly.graph_objs as go
from IPython.display import display, HTML
## Tomas Mazak's workaround
plotly.offline.init_notebook_mode()
display(HTML(
'<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_SVG"></script>'
))
##
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4],
y=[1, 4, 9, 16],
name=r'$\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}$'
))
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4],
y=[0.5, 2, 4.5, 8],
name=r'$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$'
))
fig.update_layout(
xaxis_title=r'$\sqrt{(n_\text{c}(t|{T_\text{early}}))}$',
yaxis_title=r'$d, r \text{ (solar radius)}$'
)
fig.show()