0

This is a slightly borderline question for matplotlib-xelatex combination.

I ma having an issue forcing a sans-serif font family in a png/pdf output of my matplotlib. I wanted to use tex rendering for consistency but for technical reasons wanted to output an png/pdf image rather than PGF output. Either way though fonts for labels, tick labels and legend are all serif even though I am trying to get sans-serif.

Here's a minimal example:

from numpy import linspace
import matplotlib
matplotlib.use('pgf')
from matplotlib.pyplot import close, figure, rcParams

params = {
    'font.family': 'sans-serif',
    'font.sans-serif': 'Linux Biolinum',
    'text.usetex': True,
    'pgf.texsystem': "xelatex",
    'pgf.rcfonts': True,
    'text.latex.preamble': [
        r'\usepackage{libertine}',
        r'\usepackage{fontspec}',
        r'\setmainfont{Linux Biolinum}',
    ]
}
rcParams.update(params)
my_figure = figure()
x = linspace(-1, 1)
axes = my_figure.gca()
axes.plot(x, x**2, label='\\textrm{Re}')
axes.legend(loc='upper left', handlelength=2)
my_figure.savefig(
    'test.png',
    box_inches='tight',
    dpi=300)
close(my_figure)
  • Have you tried something like \renewcommand*\familydefault{\sfdefault}? – DG' Sep 7 at 7:46
  • Thanks for the comment. I have tried to add it to the preamble, but I am still getting serif fonts. – Robert Sawko Sep 7 at 7:59
  • Don't use \textrm! – DG' Sep 7 at 9:13
  • Gosh, you're right. This is embarassing. Too many degrees of freedom for my very little brain... I removed the roman font from the label but the tick labels still have serifs. I am now printing suptitle for comparison and I can still see the difference (labels print correctly now!). – Robert Sawko Sep 7 at 10:35
  • I tried adding sfmath package as per this suggestion: stackoverflow.com/questions/11611374/… but same effect. I know that this is pretty pedantic... – Robert Sawko Sep 7 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.