Reputation: 21
I am having issues having a consistent font style in Plotly when exporting the plot containing LaTeX to PDF.
See the following link for the same issue that was apparently never resolved:
Plotly LaTeX fonts different in Jupyter and exported PDF or PNG
Essentially, it happens whenever r"$latex variable$
is used instead of "normal text"
. If you run the following code, you'll notice that the LaTeX variables appear bold compared to normal text. I would appreciate help figuring out the solution. Thank you.
import plotly.graph_objects as go
# Create the plot
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[2, 4, 1, 3, 5],
mode='lines',
name='Line'
))
fig.update_layout(
title='This only happens on the PDF!',
xaxis_title=r'$\Delta \text{ should not be bold, but it is}$',
yaxis_title='Normal text is not bold',
)
fig.show()
# Save the plot as a PDF file
fig.write_image('plot_with_bold_LaTeX.pdf')
I have already tired different rendering engines such as Orca and Kaleido. I have also tried exporting to SVG and then converting to PDF, but all of the LaTeX cannot be interpreted by ImageMagick nor Inkscape. I suspect the error lies with MathJax similar to how it is described in Plotly LaTeX fonts different in Jupyter and exported PDF or PNG
OpenAI also suggested adding the following to the Plotly Layout, but it didn't work:
import plotly.graph_objects as go
import plotly.io as pio
# Create and configure your Plotly figure
fig = go.Figure()
# Add your plotly traces and layout settings here
# ...
# Configure MathJax options in the HTML template
pio.templates.default = dict(
layout=go.Layout(
template="plotly",
meta=dict(
mathjax="cdn",
mathjax_config="TeX-AMS-MML_SVG",
),
)
)
# Save the figure as a PDF file
pio.write_image(fig, 'figure.pdf', format='pdf', engine='kaleido')
Upvotes: 2
Views: 372
Reputation: 820
The xaxis title isn't bold. Only by replacing xaxis_title=r'$\Delta \text{ should not be bold but it is}$
,
with `xaxis_title=r'$\bf\Delta \text{ should not be bold, but it is}$', you'll get a
bold text.
To have a consistent font in pdf define the plot layout as follows:
fig.update_layout(width=500, height=400,font_family="Balto", font_size=11,
title='This only happens on the PDF!',
xaxis_title=r'$\Delta \text{ should not be bold, but it is}$',
yaxis_title='Normal text is not bold',
)
Upvotes: -1