3

I am using the pgf backend in matplotlib 1.5.3 to produce publication-quality figures. Here is how I set up the plotting script.

import matplotlib as mpl

mpl.use('pgf')
pgf_with_latex = {                      # setup matplotlib to use latex for output
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
    "text.usetex": True,                # use LaTeX to write all text
    "font.family": "sans-serif",
    "font.sans-serif": "Bitstream Vera Sans, Helvetica, Computer Modern Sans Serif",
    "pgf.preamble": [
        r"\usepackage[utf8x]{inputenc}",    # use utf8 fonts because your computer can handle it :)
        r"\usepackage[T1]{fontenc}",        # plots will be generated using this preamble
        r"\usepackage{textcomp}",
        r"\usepackage{sfmath}",             # sets math in sans-serif
    ]
    }
mpl.rcParams.update(pgf_with_latex)

import matplotlib.pyplot as plt

def newfig():
    plt.clf()
    fig = plt.figure(figsize=(4, 2))
    ax = fig.add_subplot(111)
    return fig, ax

fig, ax = newfig()
ax.set_xlabel("Some x-label text")
plt.gcf().tight_layout()
plt.savefig(os.getcwd() + "/test.pdf")
plt.savefig(os.getcwd() + "/test.eps")

I am using the package sfmath to set all math environments in sans-serif. At the end I save in .pdf and .eps format. Here's the problem: While the pdfclearly uses sans-serif font everywhere, the eps file uses serif for all tick labels (not axis labels)! When I modify my LaTeX template to use sfmath it does not change the tick labels.

How can I prevent the epsfrom using serif font in the tick labels?

Edit: After a good day of experimenting, the only (barely) satisfying solution I found was to use .tiff as format, since this is also allowed by the journal. eps just seems to have problems and always turns out different than the pdf or other image formats.

Ian
  • 1,688
  • 18
  • 35
  • You may want to [edit] the question to include the matplotlib version you are using (there are differences between 1.5 and 2.0 concerning ticklabel fonts). You may also replace *the rest of the program* with something that constitutes a minimal example, that allows everyone to reproduce the issue. – ImportanceOfBeingErnest Feb 21 '17 at 15:46
  • @ImportanceOfBeingErnest Done and done. I am using matplotlib 1.5.3 – Ian Feb 21 '17 at 15:56
  • There is a square bracket missing somewhere, but the issue is reproducible in matplotlib 2.0. To me it seems that the `\usepackage{sfmath}` is simply ignored, because also formulas like `$x$` in the labels are written in serif font in the eps. Since eps files produced by matplotlib anyways [do not look very good](http://matplotlib.org/users/usetex.html#postscript-options) *("[unacceptable] results, because the text is coarsely rasterized and converted to bitmaps, which are not scalable like standard postscript, and the text is not searchable.")* you may want to create the eps externally?! – ImportanceOfBeingErnest Feb 21 '17 at 16:29
  • @ImportanceOfBeingErnest the missing bracket was a copy/paste error. I will think about handing in a `pdf` for my submission instead of an `eps`. Still, an answer would help me a lot. – Ian Feb 22 '17 at 07:22

0 Answers0