2015-11-26 57 views
1

我拼命地試圖找到'最簡單的方法'來獲得我的matplotlib數字和我的latex文檔中看起來相同的文本。matplotlib和latex之間的一致字體

到目前爲止,我的策略是:

  • 設置爲我的乳膠數字明確的寬度和設定Matplotlib大小相同。這樣設置mpl中的字體大小等於Latex字體大小應該是第一步

  • 在mpl中使用usetex = True選項。我知道這應該使mpl在圖中使用Latex進行所有文本處理(標題,標籤,文本,註釋等)。

我的問題是,最後我仍然無法設置字體,因爲我想。

下面是一個橫軸標籤字體明顯不同於刻度標籤的示例,我相信它與我的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 

和我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 

enter image description here

此外,我甚至不相信我能夠選擇/從現代計算機切換到拉丁現代,部分是因爲我的眼睛無法區分它們(這在某種程度上使問題無關緊要,但您知道.. 。極客...)。

+0

我張貼一個似乎解決了我的問題的答案,但是我仍然好奇地對我的問題「一般」有反饋意見。 –

回答

3

我已經使用了多年沒有問題;只需在我的LaTeX文檔中使用的text.latex preamble中指定相同的字體,例如:

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$') 

enter image description here

編輯我以前從未使用過的樣式表,但這似乎當我把它放在一個空的樣式表以及工作:

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

我很高興這很簡單,同時我很沮喪,我花了下午的時間... –

+0

你知道這也可以在樣式表中指定嗎? –

+0

我直到今天才知道matplotlib有/有樣式表:) – Bart

0

看來,由於一些瘋狂的原因,字體切換到新世紀學校圖書。見this SO answer

我仍然不知道發生了什麼,但似乎font.serif選項的存在改變行爲,不管這實際上是在font.familyfont.serif。這裏是一個例子(其他選項和問題一樣):

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

這給出了以下內容,其中軸標籤使用與刻度標籤相同的字體。註釋掉font.serif這條線會產生最初問題的情節。

enter image description here

相關問題