Export plotly figure such that when I include it in latex file it compiles the text with the same font as the document

I save the image in this way:

fig.write_image(“fig1.eps”) or fig.write_image(“fig1.pdf”)

when I include it in latex with this command:

\includegraphics{fig1.pdf}

I would like that the font of the image matches the font of my document. How should I do something like this?

Hi @tommyiaq ,
The LaTeX default font is a font from Serif family.
Hence in your Plotly Figure set the font_family="Serif".

Tips to save your figure as a pdf image with 300dpi:

  • In fig.layout restrict the plot margins to a few pixels; otherwise
    you’ll get a large space around the figure in your LaTeX file.
  • set font_size greater than 12 if you intend to scale the box within the LaTeX file.
  • save the pdf file of width=wnr_inches*300, height=hnr_inches*300
    With this code that generates a Plotly Figure:
import plotly.graph_objects as go
import numpy as np
x_labels= ['LaTeX', 'font', 'versus', 'Serif', 'Plotly']
fig = go.Figure(go.Bar(x= x_labels, y=np.random.randint(4, 11, 5)))
fig.update_layout(width =500, height=250, 
                  font_family="Serif", font_size=14, 
                  margin_l=5, margin_t=5, margin_b=5, margin_r=5)

and with these settings for pio.write_image:

import plotly.io as pio
#save a figure of 300dpi, width 1.5 inches, height 0.75inches
pio.write_image(fig, "test.pdf", width=1.5*300, height=0.75*300)

the LaTeX code:

\documentclass[12pt]{article}
\usepackage{graphicx}
\begin{document}

The centers change iteratively such that to minimize the so called within
cluster sum of squares. More precisely,
in each step  of the iterative procedure compute for each point $x_n$ 
the squared distance to the nearest center of that step. 
\begin{figure}[h]
\centerline{\scalebox{0.96}{\includegraphics{test.pdf}}}
\caption{\label{labe1} LaTeX font versus Serif font, set in the Plotly figure}
\end{figure}
To record the index of the nearest
 center, one defines the following function:
\end{document}

produces something like the inserted png image, below:

Thank you very much for your time! I did something like this.

import plotly.graph_objects as go

labels = ['not tagged','tagged']
values = [ 30, 70]

fig = go.Figure(data=[go.Pie(values=values, 
                             text = labels, 
                             showlegend=False)]
                             )
fig.update_layout(font = dict(family="Serif", 
                              color='black',
                              size=12)
                              )
fig.write_image("partition.pdf")

But I was wondering if there was a way to give back the text somehow “raw” so that Latex compiles it with his font. Maybe an .eps is better for the scope. I know that you can compile latex math simply by adding r’$…$’ in titles or annotations. Instead for the margin I clip the image directly in Latex.

\documentclass[12pt]{article}
\usepackage{graphicx}
\begin{document}

The centers change iteratively such that to minimize the so called within
cluster sum of squares. More precisely,
in each step  of the iterative procedure compute for each point $x_n$ 
the squared distance to the nearest center of that step. 
\begin{figure}[h]
\includegraphics[trim=0 50 0 50,clip,width=\textwidth]{partition.pdf}
\caption{\label{labe1} LaTeX font versus Serif font, set in the Plotly figure}
\end{figure}
\end{document}

Anyway I appreciate the dpi trick and thank you again.

@tommyiaq
Unfortunately your preferred solution is not possible.

I suggested to use font_family="Serif", not LaTeX strings, because the strings are not displayed correctly in all cases, as I pointed out in this answer [Making a subscript and superscript on the same character], a few days ago.

To check it, you can run these lines of code:

text =["$V_0^{0}$", "$V_1^{2}$", "$\\text{Here is: }V_2^{3}$", "$V_3^{4}$"]
fig= go.Figure(go.Bar(x=[2,3,4,5], y=[ 2,3,1, 2.6], text=text,  textposition="outside"))
fig.update_layout(width=700, height=450, yaxis_range=[0, 3.25])
    ```

Very strange that math does not compile in Bar charts. Unfair solution:

text =["$V_0^{0}$", "$V_1^{2}$", "$\\text{Here is: }V_2^{3}$", "$V_3^{4}$"]
x=[2,3,4,5]
y=[ 2,3,1, 2.6]

fig= go.Figure(go.Bar(x=x, y=y))

[fig.add_annotation(x=xi, y=yi-0.2,
                    text=t,
                    font_size=16,
                    showarrow=False) for xi,yi,t in zip(x,y,text)]

fig.update_layout(width=700, height=450, 
                  yaxis_range=[0, 3.25],
                  title="$a^n$",
                  xaxis_title=r'$\frac{1}{3}$',
                  yaxis_title = r'$\left(\frac{a+b}{c-d}\right)$')