2

I would like to have a matplotlib plot in my Latex document that uses the same font as the rest of the document. I read in the matplotlib documentation that if you set the rc parameter 'usetex' to true it will use the 'Computer Modern' font, which is also the standard for Latex. Trying this gave me the following result.

Imported matplotlib figure in Latex

The title of the plot is generated by matplotlib, the caption by the Latex document. As you can see, the font doesn't match. I think both use 'Computer Modern', but not the same font family. The title (by matplotlib) might be something like 'Sans Serif Roman', while the caption (by the Latex document) is something like 'Serif Roman'. I tried to change the font family with the following:

plt.title("Lorem Ipsum", family='serif', fontsize=20) 

But it has no effect as long usetex is activated. I also tried it with fontdict, but it also did not change the font in any way. Also writing the name of a font directly to family does not work.

Is there any way to get the same font as in the Latex document?

4
  • 1
    You may want to look at including the preamble in matplotlib's settings to have prepared images and document closer together. Commented Jul 29, 2022 at 12:23
  • 2
    Does this answer your question? How to obtain the same font(-style, -size etc.) in matplotlib output as in latex output? Commented Jul 29, 2022 at 12:23
  • 1
    Over time, I've moved away from using prepared images from matplotlib to including plots etc. directly in the LaTeX document with the help of the tikz package. That way, you're guaranteed to have exactly the same font appearance. If stuff takes too long to render, check out tikzexternal, which caches tikz graphics. Commented Jul 29, 2022 at 12:28
  • @snwflk Thanks! I tried with tikz too, but I had several problems, so I went back to pdf import. I will definitely check out tikzexternal if I try it again. Commented Jul 29, 2022 at 12:45

2 Answers 2

3

Ok, after speding yesterday half a day with searching a solution, I now stumbled over it 5 minutes after asking the question.

Solution:

plt.rcParams['font.family'] = 'serif' 

For some reason when usetex=True, setting the fontfamily works only globally.

Sign up to request clarification or add additional context in comments.

Comments

0
import matplotlib.pyplot as plt plt.style.use('classic') plt.style.use('dark_background') 

Example:

import matplotlib.pyplot as plt import numpy as np from scipy import special plt.style.use('classic') plt.style.use('dark_background') s = np.linspace(-50,40,150) p0 = special.polygamma(0,s) p1 = special.polygamma(1,s) p2 = special.polygamma(2,s) plt.plot(s,p2,linewidth=1,c='blue') plt.plot(s,p1,linewidth=1,c='green') plt.plot(s,p0,linewidth=1,c='red') plt.ylim(-100,100) plt.annotate(r'$\psi^{n}(s)=\frac{d^{n}}{dx^{n}}\ln\Gamma(s)$',xy=(5,-25),size=20,fontweight='bold') plt.annotate(r'$\psi^{n}(s)=(-1)^{n+1}n!\zeta(n+1,s)$'+'\n'+'$\{s \in \mathbb{N} : s \geq 1\}$',xy=(1,-75),size=15,fontweight='bold') plt.axhline(y=0,color='white',linewidth=.5) plt.axvline(x=0,color='white',linewidth=.5) plt.grid(lw=.5,ls=':') plt.xlabel('s',fontweight='bold') plt.ylabel(r'$\psi^{n}(s)$') 

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.