简体   繁体   English

matplotlib + latex + 自定义ttf字体

[英]matplotlib + latex + custom ttf font

I have to make a figure in python.我必须在 python 中制作一个数字。 I need it to use the font Palatino.我需要它来使用 Palatino 字体。 I downloaded the font here .我在这里下载了字体。 I placed it under *\matplotlib\mpl-data\fonts\ttf (which turned out to be useless since I had to provide full path to make it work).我把它放在 *\matplotlib\mpl-data\fonts\ttf 下(结果证明它没有用,因为我必须提供完整的路径才能让它工作)。

Using the following lines allows me to use the font:使用以下行允许我使用字体:

prop = fm.FontProperties(fname='C:/Users/MyPC/pyApp/venv/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/Palatino-Roman.ttf')
mpl.rcParams['font.family'] = prop.get_name()

Yay.耶。 Now when I want to use Latex in matplotlib,现在当我想在 matplotlib 中使用 Latex 时,

rc('text',usetex=True)

the font is now not the one I want.字体现在不是我想要的字体。 I tried to follow the official page about that and instead use:我试图关注官方页面,而是使用:

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

but I cannot see any difference.但我看不出有什么区别。 I tried all possibilities and it looks like the same font.我尝试了所有可能性,它看起来像相同的字体。 What am I doing wrong?我究竟做错了什么? Perhaps its the latex side that's lacking the required font package...也许它的 latex 端缺少所需的字体 package ...

You can load any latex packages when using rc('text',usetex=True)使用rc('text',usetex=True)时,您可以加载任何 Z25F7E525B5C0641E1EB1FB3BDEBEB15B5Z 包

You can add this in your code:您可以在代码中添加它:

plt.rcParams['text.latex.preamble'] = [r'\usepackage{palatino, mathpazo}']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM