Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a per mille sign in my axis title using Latex in matplotlib?

Until now I have this:

plt.xlabel('$\delta^1^8$O \‰ vs VSMOW') 

It runs fine without the promille sign, however when I add it a blank graph appears.

Then I tried TheImportanceOfBeingErnest's answer:

plt.xlabel(u'$\delta^{18}$O ‰ vs VSMOW') 

But then this showed up:

"UnicodeEncodeError: 'ascii' codec can't encode character '\u2030' in position 264: ordinal not in range(128)"

Maybe there is something wrong with this?

from matplotlib import rc

rc('font', **{'family':'serif','serif':['Palatino']})
    rc('text', usetex=True)

Solution (thanks to TheImportanceOfBeingErnest)

plt.rcParams['text.latex.preamble']=[r"\usepackage{wasysym}"]

and

plt.xlabel(r'$\delta^{18}$O \textperthousand vs VSMOW')
like image 832
Oscar van Alphen Avatar asked May 29 '17 12:05

Oscar van Alphen


1 Answers

Using normal text

You need to

  1. use a unicode string, i.e. u"string"
  2. not escape the character, i.e. exists, but \‰ does not.

So

plt.xlabel(u'$\delta^{18}$O ‰ vs VSMOW') 

produces

enter image description here

Using latex

Latex does not have a permille sign built in. Two ways to go would be

  • In text mode: use the \usepackage{textcomp} package and get it via \textperthousand,

    plt.xlabel(r'$\delta^{18}$O \textperthousand vs VSMOW') 
    
  • In math mode: use the package \usepackage{wasysym} and get it via \permil.

    plt.xlabel(r'$\delta^{18}$O $\permil$ vs VSMOW') 
    

    Using the first package and using \text{\textperthousand} inside math mode should work as well.

enter image description here

For how to get those packages into matplotlib, read How to write your own LaTeX preamble in Matplotlib?

like image 57
ImportanceOfBeingErnest Avatar answered Oct 03 '22 05:10

ImportanceOfBeingErnest