简体   繁体   English

matplotlib和乳胶之间的一致字体

[英]Consistent fonts between matplotlib and latex

I'm desperately trying to find 'the easiest way' to obtain identically looking text in my matplotlib figures and my latex document. 我正拼命试图在matplotlib图形和乳胶文档中找到“最简单的方法”来获得外观相同的文本。

So far my strategy is to: 到目前为止,我的策略是:

  • set an explicit width for my Latex figures and set the same size with Matplotlib. 为我的乳胶数字设置一个显式宽度,并与Matplotlib设置相同的大小。 That way setting a font size in mpl equal to the Latex font size should be a first step 这样,在mpl中将字体大小设置为等于Latex字体大小应该是第一步

  • use the usetex = True option in mpl. 在mpl中使用usetex = True选项。 I understand that this should make mpl use Latex for ALL text processing in the figure (title, labels, text, annotation, etc.). 我知道这应该使mpl使用Latex进行图中的所有文本处理(标题,标签,文本,注释等)。

My problem is that in the end I still can't set the fonts as I'd like to. 我的问题是,最终我仍然无法按照自己的意愿设置字体。

Here is an example where the horizontal axis label font is clearly different from the ticks labels, which I'm confident are identical to the text of my latex document. 这是一个示例,其中水平轴标签字体与刻度标签明显不同,我确信它们与我的乳胶文档的文本相同。

Here is the code that generated the figure: 这是生成图形的代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

with plt.style.context('journal'):
  fig_size = [3.4, 2.1]
  fig = plt.figure(1, figsize=fig_size)
  ax1 = plt.subplot(111)
  ax1.plot([1,2,3,4,5], '+--')
  ax1.set_xlabel(r'1 2 3 4.0 intensity [$10^{13}p$]')
  plt.tight_layout()
  fig.savefig('test.png') # I saved to png for SO but the goal is eps

and my journal mpl stylesheet is 我的journal mpl样式表是

backend: PS
text.usetex: True
text.latex.unicode: True
font.family: lmodern # apparently no effect as usetex is true anyway
font.size: 10
axes.titlesize: 10
axes.labelsize: 10
axes.unicode_minus: True
axes.linewidth: 1
legend.fontsize: 10
xtick.labelsize: 10
xtick.major.size: 4
xtick.major.width: 0.5
ytick.labelsize: 10
lines.linewidth: 1.5
lines.markersize: 3
figure.titleweight: normal
savefig.dpi: 600

在此处输入图片说明

Additionally, I'm not even sure that I'm able to choose/switch from Computer Modern to Latin Modern, partly because my eyes can't distinguish them (which in a way makes the issue irrelevant, but you know... geeks...). 此外,我什至不确定我是否能够选择/从Computer Modern切换到Latin Modern,部分原因是我的眼睛无法区分它们(这在某种程度上使问题无关紧要,但是您知道...怪胎...)。

I have been using this for years without a problem; 我已经使用多年了,没有任何问题。 simply specifying the same font in the text.latex preamble as I'm using in my LaTeX document, eg: 只需在text.latex preamble指定与我在LaTeX文档中使用的字体相同的字体,例如:

import matplotlib.pylab as pl
from matplotlib import rc
rc('text', usetex=True)
rc('font', size=14)
rc('legend', fontsize=13)
rc('text.latex', preamble=r'\usepackage{cmbright}')

pl.figure()
pl.plot([0,1], [0,1], label=r'$\theta_{\phi}^3$')
pl.legend(frameon=False, loc=2)
pl.xlabel(r'change of $\widehat{\phi}$')
pl.ylabel(r'change of $\theta_{\phi}^3$')

在此处输入图片说明

edit I've never used stylesheet before, but this seems to work as well when I put it in an empty stylesheet: 编辑我以前从未使用过样式表,但是当我将其放入空样式表时,这似乎也能正常工作:

text.usetex: True
font.size: 14
legend.fontsize: 13
text.latex.preamble: \usepackage{cmbright}

It seems that for some insane reason the font was switched to New Century School Book . 似乎出于某种疯狂的原因,字体被切换到了New Century School Book See this SO answer . 看到这个答案

I'm still not sure what is happening but it seems that the presence of the font.serif option changes the behaviour, irrespective of what is actually in font.family or in font.serif . 我仍然不确定发生了什么,但是无论font.familyfont.serif实际上是什么, font.serif选项的出现似乎font.serif改变行为。 Here is an example (other options as in the question): 这是一个示例(问题中的其他选项):

backend: PS
text.usetex: True
text.latex.unicode: True
font.family: xyzxyz
font.serif: abcabc # Comment this out

Which gives the following, where the axis label is using the same font as the tick labels. 这给出了以下内容,其中轴标签使用与刻度标签相同的字体。 Commenting out the font.serif line produces the plot of the initial question. 注释掉font.serif行会产生初始问题的图。

在此处输入图片说明

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

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