Font in latex mode

Hi,

I am trying to make an article quality plot with plotly library. The code is given below. And I face several problems.

  1. The plots produced by plotly in jupyter-lab environment and stored with write_image() function into a PDF file are slightly different. The PDF files cannot be uploaded here, so I generated a PNG file, which is again slightly differs.
  2. I want to use latex math in the legend box. But I am not sure what shall I choose to match the font family/size to the one used for axis ticks. It looks like the legend text font is bold in math mode, and this is not desired. The best match of the ticks size to the legend font size is achieved with "\\large latex environment.

So basically I need:

  • to use latex math mode for legend (and also axis labels, which I omitted for simplicity)
  • consistent choice of font family/size across legend/titles

Any hints how can I achieve that?
Thanks a lot in advance!

import plotly.graph_objects as go

fig = go.FigureWidget()
fig.layout.legend.font.size = 16
fig.layout.font.size = 18

for v in ['xaxis','yaxis']:
    o = getattr(fig.layout,v)
    setattr(o,'gridcolor','white')
    setattr(o,'linecolor','black')
    setattr(o,'titlefont',dict(size=16))
    setattr(o,'zerolinecolor','#444')

fig.layout.legend.yanchor = 'top'
fig.layout.legend.y = 0.99
fig.layout.legend.xanchor = 'left'
fig.layout.legend.x = 0.01

vx = [v/100 for v in range(-20,40+1)]

fig.add_scatter(
    x=vx,
    y=[x for x in vx],
    name='No latex: x'
)
fig.add_scatter(
    x=vx,
    y=[x**2 for x in vx],
    name='$Latex: \\quad x^2$'
)
fig.add_scatter(
    x=vx,
    y=[x**3 for x in vx],
    name='$\\textrm{textrm Latex:} x^3$'
)
fig.add_scatter(
    x=vx,
    y=[x**4 for x in vx],
    name='$\\large\\textrm{large textrm Latex:} x^4$'
)

fig.layout.xaxis.tickformat = '%'

fig.write_image(f'figs/test.pdf')
fig

For those interested, with the latest Plotly.js v2.10.0, you now have support for Mathjax. Here are a coupe of examples for Latex with Plotly.