24

The following script:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as mpl

mpl.rc('font', family='sans-serif')
mpl.rc('text', usetex=True)

fig = mpl.figure()
ax = fig.add_subplot(1,1,1)
ax.text(0.2,0.5,r"Math font: $451^\circ$")
ax.text(0.2,0.7,r"Normal font (except for degree symbol): 451$^\circ$")

fig.savefig('test.png')

is an attempt to use a sans-serif font in matplotlib with LaTeX. The issue is that the math font is still a serif font (as indicated by the axis numbers, and as demonstrated by the labels in the center). Is there a way to set the math font to also be sans-serif?

astrofrog
  • 32,883
  • 32
  • 90
  • 131
  • See http://stackoverflow.com/questions/17958485/matplotlib-not-using-latex-font-while-tex-usetex-true/17967324?noredirect=1#17967324 – Dirklinux Aug 01 '13 at 12:43
  • does my answer below work? – Paul H Apr 21 '15 at 20:52
  • You may want to check out https://github.com/nschloe/matplotlib2tikz. Also, matplotlib was recently equipped with a TikZ/PGF backend, cf. https://github.com/matplotlib/matplotlib/issues/319. – Nico Schlömer May 11 '13 at 19:55

3 Answers3

32

I always have text.usetex = True in my matplotlibrc file. In addition to that, I use this as well:

mpl.rcParams['text.latex.preamble'] = [
       r'\usepackage{siunitx}',   # i need upright \micro symbols, but you need...
       r'\sisetup{detect-all}',   # ...this to force siunitx to actually use your fonts
       r'\usepackage{helvet}',    # set the normal font here
       r'\usepackage{sansmath}',  # load up the sansmath so that math -> helvet
       r'\sansmath'               # <- tricky! -- gotta actually tell tex to use!
]  

Hope that helps.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
Paul H
  • 65,268
  • 20
  • 159
  • 136
  • This is exactly what I needed to get the (keyword:) Helvetica font working with matplotlib/pylab in my plots. And I have tried quite a few things! Hopefully google will index this incase others try to do the same. – SullX Apr 29 '14 at 02:24
  • 1
    Perfect. For more font options, look at http://www.tug.dk/FontCatalogue/sansseriffonts.html and replace the '\usepackage{helvet}' with your favourite :) – Niko Föhr Mar 02 '16 at 18:52
  • For an alternate syntax using keyword arguments, see https://stackoverflow.com/a/41453758/8622053 – Daniel Chin Nov 16 '22 at 06:51
14

The easiest way is to use matplotlib's internal TeX, e.g.:

import pylab as plt
params = {'text.usetex': False, 'mathtext.fontset': 'stixsans'}
plt.rcParams.update(params)

If you use an external LaTeX, you can use, e.g., CM Bright fonts:

params = {'text.usetex': True, 
          'text.latex.preamble': [r'\usepackage{cmbright}', r'\usepackage{amsmath}']}
plt.rcParams.update(params)

Note, that the CM Bright font is non-scalable, and you'll not be able to save PDF/PS! Same with other options with external LaTeX I've found so far.

Vyacheslav
  • 151
  • 1
  • 4
2

This will enable you to use the Computer Modern Sans font if you, as I, prefer it over Helvetica:

mpl.rcParams['text.usetex'] = True 
mpl.rcParams['text.latex.preamble'] = [r'\usepackage[cm]{sfmath}']
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'cm'
emprice
  • 912
  • 11
  • 21
nedim
  • 1,767
  • 1
  • 18
  • 20
  • This worked for me, and I'd been struggling to find what the computer modern fonts were called in the backend. I couldn't find a list anywhere. – thosphor Feb 21 '17 at 10:18