我更新了蟒蛇的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')
使用膠乳作爲軸標籤,如在上面的代碼中,導致在軸不一致的文本的圖(見下面圖片)。
如何找回以前的行爲(見下圖),或爲一致字體方案?
編輯: 使用乳膠後端我能得到一個好的結果,但它是極其緩慢。 無論如何,我認爲內部後端應該能夠獲得一致的輸出,並且切換到不同的後端並不是真正的解決方案,而是更多的解決方法。
要使用乳膠後端:
#%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的結果是:
與舊版本生成的情節(還是有點不同的,也許由於一些乳膠的差異):
但同樣,期望的結果是從舊版本matplotlib獲得什麼,在顯示在圖2
您鏈接到的文檔中提到「使用內置數學渲染引擎(mathtext)時的默認數學字體已從」Computer Modern「(即LaTeX-like)更改爲」DejaVu Sans「。換句話說,_default_行爲應該像sans-serif字體一樣,像你的底部圖。它看起來像'mathtext.rm':'serif'是原因,刪除它應該解決問題。 – roganjosh
我試過這個改變,但輸出是一樣的。 – Alex
我無法複製,因此我嘗試升級matplotlib,現在它非常有用地刪除了我的'numpy'安裝,所以我無法測試任何東西。但數學字體部分還提到''mathtext.fontset':'cm''所以也許刪除。我無法弄清楚那條線是幹什麼的。 – roganjosh