3

I wanted to use a euro symbol in my axis descriptions for matplotlib plots saved as pgf files with the pgf backend. When I use the following code:

import numpy as np
import matplotlib.pyplot as mplot
import matplotlib.cm as cm
import matplotlib
import scipy.optimize

def f(x,sign=1.0):
        out = sign*(5*x + 6e-1*np.pi*(1/x))
        return out

mplot.close('all')
x = np.linspace(0.1,1)
y = f(x)
mplot.plot(x,y)
mplot.grid(1)
mplot.axis([0,1,0,20])
mplot.xlabel(r'$\log(\frac{d_a}{d_i})$')
mplot.ylabel(r'$\frac{\euro}{a}$')
mplot.savefig('minimum.pgf')

The result is the following error:

ValueError: Error processing '\(\displaystyle \frac{\euro}{a}\)'
LaTeX Output:
! Undefined control sequence.
<argument> \euro 

<*> ...lectfont \(\displaystyle \frac{\euro}{a}\)}

No pages of output.
Transcript written on texput.log.

The commonly suggested solution is to have matplotlib use LaTeX throughout:

# coding: utf-8

# In[2]:

import numpy as np
import matplotlib.pyplot as mplot
import matplotlib.cm as cm
import matplotlib
from mpl_toolkits.mplot3d import Axes3D as m3d
import scipy.optimize


# In[3]:

#matplotlib.use("pgf")
pgf_with_xelatex = {
'text.usetex': True,
'text.latex.unicode': True,
'pgf.rcfonts': False,
'font.family': 'sans-serif',
"pgf.texsystem": "xelatex",
"pgf.preamble": [
r"\usepackage{amssymb}",
r"\usepackage{amsmath}",
r"\usepackage{fontspec}",
r"\setmainfont{Gentium Book Basic}",
r"\setsansfont{Open Sans Light}",
r"\usepackage{unicode-math}",
r"\setmathfont{TeX Gyre Termes Math}"
]
}

matplotlib.rcParams.update(pgf_with_xelatex)


# In[4]:

def f(x,sign=1.0):
    out = sign*(5*x + 6e-1*np.pi*(1/x))
    return out


# In[5]:

mplot.close('all')
x = np.linspace(0.1,1)
y = f(x)
mplot.plot(x,y)
mplot.grid(1)
mplot.axis([0,1,0,20])
mplot.xlabel(r'$\log(\frac{d_a}{d_i})$')
mplot.ylabel(r'$\frac{\euro}{a}$')
mplot.savefig('minimum.pgf')

The python code executes without errors, but there are two problems.

  1. The euro symbol is missing from the matplotlib figure (see the ordinate axis) and everything is in serif, when I expect it to be sans-serif:

matplotlib qt window

  1. The resulting pgf image has the euro symbol, but also has everything in serif and does not appear to be honouring my font selections:

matplotlib pgf output

A similar pgf image produced with matplotlib but without specifying LaTeX in the matplotlib configuration produces this plot:

example with desired axis tick font

The axis tick font is Open Sans Light. I want to use a serif math font for the axis descriptions and leave everything else the same.

The sans-serif tick text is specified in pgf with these lines:

\begin{pgfscope}%
\pgftext[x=2.550000in,y=0.544444in,,top]{{\sffamily\fontsize{12.000000}{14.400000}\selectfont 5}}%
\end{pgfscope}%

In the pgf with serif ticks, the tick text is specified like so:

\begin{pgfscope}%
\pgftext[x=1.000000in,y=0.544444in,,top]{{\sffamily\fontsize{12.000000}{14.400000}\selectfont \(\displaystyle 0.0\)}}%
\end{pgfscope}%

Here is my LaTeX MWE for the pgf graphics:

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgf}
\usepackage{graphicx}
\usepackage{unicode-math}
\usepackage{array}
\usepackage{fonts}

\DeclareMathOperator*{\argmax}{arg max}

\begin{tabular}{m{5cm}m{0.8\textwidth}}
\raggedleft \begin{equation*} \argmax_{x} f_{\mathrm{H}1}(x) = \begin{pmatrix} 8.6997998 \\ 6.7664998 \end{pmatrix} 
\end{equation*} & \resizebox{0.8\textwidth}{!}{\input{minimum.pgf}}
\end{tabular}
\end{document}

The fonts.sty contains:

\setmathfont{TeX Gyre Termes Math}
\setmainfont{Gentium Book Basic}
\setsansfont{Open Sans Light}
  • 2
    You're math font is Tex Gyre Termes, whicH is a serif font. – MaxNoe Jan 8 '15 at 14:14
  • @MaxNoe That's what I want, though -- the math in the axis descriptions is serif. What about everything else? Or maybe I should just say, how do I get the ticks to be sans-serif, but everything in math-mode be serif? – Stephen Bosch Jan 8 '15 at 14:33
  • So what should be sans-serif? The tick labels? – MaxNoe Jan 8 '15 at 14:35
  • @MaxNoe yes, the tick labels should be sans-serif. – Stephen Bosch Jan 8 '15 at 14:36
  • 1
    @StephenBosch the tick labels are set in math mode by default. If you want them in text mode, you can use xticklabel={\pgfmathprintnumber{\tick}} in the axis options, but note that this will give a hyphen instead of a minus sign if there are any tick labels less than zero. – Paul Gessler Apr 26 '15 at 1:31

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.