我更新了蟒蛇的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