Ced*_* H. 2 python fonts matplotlib
我拼命地试图找到“最简单的方法”,以在我的 matplotlib 图形和乳胶文档中获得外观相同的文本。
到目前为止,我的策略是:
为我的 Latex 图形设置一个明确的宽度,并使用 Matplotlib 设置相同的大小。这样在 mpl 中设置字体大小等于 Latex 字体大小应该是第一步
使用usetex = Truempl 中的选项。我知道这应该使 mpl 使用 Latex 进行图中的所有文本处理(标题、标签、文本、注释等)。
我的问题是,最终我仍然无法按照自己的意愿设置字体。
这是一个示例,其中水平轴标签字体与刻度标签明显不同,我确信它们与我的乳胶文档的文本相同。
这是生成图形的代码:
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
Run Code Online (Sandbox Code Playgroud)
我的journalmpl 样式表是
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
Run Code Online (Sandbox Code Playgroud)
此外,我什至不确定我是否能够选择/从现代计算机切换到拉丁现代,部分原因是我的眼睛无法区分它们(这在某种程度上使问题变得无关紧要,但你知道......极客……)。
我多年来一直使用它没有问题; 只需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$')
Run Code Online (Sandbox Code Playgroud)
编辑我以前从未使用过样式表,但是当我把它放在一个空的样式表中时,这似乎也能正常工作:
text.usetex: True
font.size: 14
legend.fontsize: 13
text.latex.preamble: \usepackage{cmbright}
Run Code Online (Sandbox Code Playgroud)