Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python latex font xcode

I'm trying to use Matplotlib & Python in Xcode to generate scientific graphics. My boss would like them to be in LaTeX with matching fonts. I know you can modify the fonts in python with something like this:

from matplotlib import rc
rc('font',**{'family':'serif','serif':['Computer Modern Roman']})
rc('text', usetex=True)

Unfortunately, opening or saving the figure with plt.show() or plt.savefig() gives a string of errors, eventually leading to OSError: [Errno 2] No such file or directory.

I know "Google is your friend", but I haven't managed to find anything on how to go about solving this. Any help would be greatly appreciated.

like image 551
pgierz Avatar asked Jun 06 '26 07:06

pgierz


1 Answers

I usually find it easiest to directly specify the font file I'm after. Here's an example:

import matplotlib.pyplot as plt
import matplotlib.font_manager
from numpy import *

# cmunso.otf is a randomly selected font in my tex installation
path = '/usr/local/texlive/2012/texmf-dist/fonts/opentype/public/cm-unicode/cmunso.otf'
f0 = matplotlib.font_manager.FontProperties()    
f0.set_file(path)

plt.figure()
plt.xlim(0,1.)
plt.ylim(0,1.)

d = arange(0, 1, .1)
plt.plot(d, d, "ob", label='example')

plt.text(.5, .1, 'text.. abcdef', fontproperties=f0, size=30)
plt.xlabel("x label", fontproperties=f0)
plt.legend(prop=f0, loc=2)

enter image description here

I'm not a font expert, but I think the reason this is easier is that font selection often has a cascading set of defaults for when the way you specify the font doesn't exactly match the way the system does. The file, though, is easy to find and specify exactly (though it's obviously less portable).

Note that for xlabel and text the keyword is fontproperties and for legend it's prop.

like image 111
tom10 Avatar answered Jun 07 '26 22:06

tom10