3

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.

4

1 回答 1

1

我通常发现直接指定我所追求的字体文件是最容易的。这是一个例子:

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)

在此处输入图像描述

我不是字体专家,但我认为这更容易的原因是当您指定字体的方式与系统的方式不完全匹配时,字体选择通常有一组级联的默认值。但是,该文件很容易找到并准确指定(尽管它显然不太便携)。

请注意, forxlabeltext关键字是fontpropertiesand for legendit prop

于 2013-07-27T22:52:10.837 回答