2017-02-15 85 views
6

我更新了蟒蛇的Python到最新版本(4.3),在那裏他們升級到Matplotlib版本2Matplotlib 2不一致字體

升級作出了默認樣式(see here)一些重大變化。 而且,雖然我真的很喜歡其中的一些變化,但我並不同意其中的一些變化。

因此我做了一些改進,如在上面的鏈接提示:

#%matplotlib inline 
#%config InlineBackend.figure_format = 'svg' 
import scipy as sc 
import matplotlib.pyplot as plt 
import matplotlib 

# http://matplotlib.org/users/dflt_style_changes.html 
params = {'legend.fontsize': 18, 
      'axes.labelsize': 18, 
      'axes.titlesize': 18, 
      'xtick.labelsize' :12, 
      'ytick.labelsize': 12, 
      'mathtext.fontset': 'cm', 
      'mathtext.rm': 'serif', 
      'grid.color': 'k', 
      'grid.linestyle': ':', 
      'grid.linewidth': 0.5, 
     } 
matplotlib.rcParams.update(params) 

x = sc.linspace(0,100) 
y = x**2 
fig = plt.figure('Fig') 
ax = fig.add_subplot(1, 1, 1) 
lines = ax.semilogy(x, y) 
ax.set_yticks([300], minor=True) 
ax.yaxis.grid(True, which='minor') 
ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter()) 
ax.tick_params(axis='y', pad=10) 
ax.set_xlabel(r'$\mathrm{R_L}$') 
ax.set_ylabel(r'$\sigma \int_l \; dx$') 
#fig.savefig('./PNG/test.png', dpi=300, bbox_inches='tight') 

使用膠乳作爲軸標籤,如在上面的代碼中,導致在軸不一致的文本的圖(見下面圖片)。

enter image description here

如何找回以前的行爲(見下圖),或爲一致字體方案?

enter image description here

編輯: 使用乳膠後端我能得到一個好的結果,但它是極其緩慢。 無論如何,我認爲內部後端應該能夠獲得一致的輸出,並且切換到不同的後端並不是真正的解決方案,而是更多的解決方法。

要使用乳膠後端:

#%matplotlib inline 
#%matplotlib notebook 
#%config InlineBackend.figure_format = 'svg' 
import scipy as sc 
import matplotlib.pyplot as plt 
import matplotlib 

# http://matplotlib.org/users/dflt_style_changes.html 
params = {'legend.fontsize': 18, 
      'axes.labelsize': 18, 
      'axes.titlesize': 18, 
      'xtick.labelsize' :12, 
      'ytick.labelsize': 12, 
      'mathtext.fontset': 'cm', 
      'mathtext.rm': 'serif', 
      'grid.color': 'k', 
      'grid.linestyle': ':', 
      'grid.linewidth': 0.5, 
     } 
matplotlib.rcParams.update(params) 
matplotlib.rcParams.update({'text.usetex':True, 'text.latex.preamble':[r'\usepackage{amsmath, newtxmath}']}) 

x = sc.linspace(0,100) 
y = x**2 
fig = plt.figure('Fig') 
ax = fig.add_subplot(1, 1, 1) 
lines = ax.semilogy(x, y) 
ax.set_yticks([300], minor=True) 
ax.yaxis.grid(True, which='minor') 
ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter()) 
ax.tick_params(axis='y', pad=10) 
ax.set_xlabel(r'$\mathrm{R_L}$') 
ax.set_ylabel(r'$\sigma \int_l \; dx$') 
#fig.savefig('./PNG/test.png', dpi=300, bbox_inches='tight') 

與matplotlib 2的結果是:

enter image description here

與舊版本生成的情節(還是有點不同的,也許由於一些乳膠的差異):

enter image description here

但同樣,期望的結果是從舊版本matplotlib獲得什麼,在顯示在圖2

+0

您鏈接到的文檔中提到「使用內置數學渲染引擎(mathtext)時的默認數學字體已從」Computer Modern「(即LaTeX-like)更改爲」DejaVu Sans「。換句話說,_default_行爲應該像sans-serif字體一樣,像你的底部圖。它看起來像'mathtext.rm':'serif'是原因,刪除它應該解決問題。 – roganjosh

+0

我試過這個改變,但輸出是一樣的。 – Alex

+0

我無法複製,因此我嘗試升級matplotlib,現在它非常有用地刪除了我的'numpy'安裝,所以我無法測試任何東西。但數學字體部分還提到''mathtext.fontset':'cm''所以也許刪除。我無法弄清楚那條線是幹什麼的。 – roganjosh

回答

0

雖然試圖找到一個解決我的問題,我想比較新舊rcParams的字典和設置這是一些不同的元素,並mathtext字體相關:結果是相當好。

的代碼是:

#%matplotlib inline 
#%matplotlib notebook 
#%config InlineBackend.figure_format = 'svg' 
import scipy as sc 
import matplotlib.pyplot as plt 
import matplotlib 

# http://matplotlib.org/users/dflt_style_changes.html 
params = {'legend.fontsize': 18, 
      'axes.labelsize': 18, 
      'axes.titlesize': 18, 
      'xtick.labelsize' :12, 
      'ytick.labelsize': 12, 
      'mathtext.fontset': 'cm', 
      'mathtext.rm': 'serif', 
      'mathtext.bf': 'serif:bold', 
      'mathtext.it': 'serif:italic', 
      'mathtext.sf': 'sans\\-serif', 
      'grid.color': 'k', 
      'grid.linestyle': ':', 
      'grid.linewidth': 0.5, 
     } 
matplotlib.rcParams.update(params) 
#matplotlib.rcParams.update({'text.usetex':True, 'text.latex.preamble':[r'\usepackage{amsmath, newtxmath}']}) 
#matplotlib.rcParams.update({'text.usetex':True, 'text.latex.preamble':[r'\usepackage{amsmath, mathptmx}']}) 
#matplotlib.rcParams.update({'text.usetex':True, 'text.latex.preamble':[r'\usepackage{amsmath}']}) 

x = sc.linspace(0,100) 
y = x**2 
fig = plt.figure('Fig') 
ax = fig.add_subplot(1, 1, 1) 
lines = ax.semilogy(x, y) 
ax.set_yticks([300], minor=True) 
ax.yaxis.grid(True, which='minor') 
ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter()) 
ax.tick_params(axis='y', pad=10) 
ax.set_xlabel(r'$\mathrm{R_L}$') 
ax.set_ylabel(r'$\sigma \int_l \; dx$') 
fig.savefig('./PNG/test.png', dpi=300, bbox_inches='tight') 

因此也增加了:

  'mathtext.rm': 'serif', 
      'mathtext.bf': 'serif:bold', 
      'mathtext.it': 'serif:italic', 
      'mathtext.sf': 'sans\\-serif', 

導致:

enter image description here

,我認爲一個乳膠文檔中相當不錯的,一致的。其他answer in this thread from @ImportanceOfBeingErnest也很整齊美觀。

1

從鏈接你沒提供:

A ‘classic’ style sheet is provided so reverting to the 1.x default values is a single line of python

mpl.style.use('classic')

添加此行

matplotlib.style.use('classic') 

到你的腳本應該解決你的問題。

我測試了它在我的python2.7/matplotlib 2上,它工作正常(即我找回了matplotlib 1.x字體)。

+0

是的,但它是真正的舊式。你沒有獲得與第二個情節相同的風格。 我想保留新的樣式並修復字體問題。 – Alex

+0

@亞歷克斯:我以爲你的意思是軸蜱只。有關字體的更多詳細信息,請參閱http://matplotlib.org/users/mathtext.html#fonts。改變你的代碼到'mathtext.fontset':'stixsans','和/或''mathtext.rm':'sansserif','對我產生很好的結果。 – Eiffel

+0

但標籤中的乳膠具有不同的字體,這是醜陋的。 – Alex

4

如果一致性是唯一的問題,您可以使用「羅馬」風格使用「時代」字體。沒有必要通過usetex使用乳膠。相反,只需使用STIX字體集,Times字體和襯線數學文本。

import scipy as sc 
import matplotlib.style 
import matplotlib.pyplot as plt 

params = {'legend.fontsize': 18, 
      'axes.labelsize': 18, 
      'axes.titlesize': 18, 
      'xtick.labelsize' :12, 
      'ytick.labelsize': 12, 
      'grid.color': 'k', 
      'grid.linestyle': ':', 
      'grid.linewidth': 0.5, 
      'mathtext.fontset' : 'stix', 
      'mathtext.rm'  : 'serif', 
      'font.family'  : 'serif', 
      'font.serif'  : "Times New Roman", # or "Times"   
     } 
matplotlib.rcParams.update(params) 

x = sc.linspace(0,100) 
y = x**2 
fig = plt.figure('Fig') 
ax = fig.add_subplot(1, 1, 1) 
lines = ax.semilogy(x, y) 

ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter()) 
ax.tick_params(axis='y', pad=10) 
ax.set_yticks([300], minor=True) 
ax.yaxis.grid(True, which='minor') 
ax.set_xlabel(r'$\mathrm{R_L}$') 
ax.set_ylabel(r'$\sigma \int_l \; dx$') 
plt.tight_layout() 
plt.show() 

enter image description here

+0

整潔,輸出很好,一致。與此同時,我嘗試比較rcParam的字典,並嘗試設置不同的元素:結果非常好。我會盡量把這個作爲可能的答案給我自己(不接受我自己的回答,因爲我不覺得它是明確的)。 – Alex