python – Matplotlib 2不一致的字体
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Matplotlib 2不一致的字体,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3464字,纯文字阅读大概需要5分钟。
内容图文
![python – Matplotlib 2不一致的字体](/upload/InfoBanner/zyjiaocheng/700/3e1e90eefd34433f8f748f84e3c04f17.jpg)
我将Anaconda Python更新到最新版本(4.3),他们将Matplotlib升级到版本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')
使用Latex作为轴标签,如上面的代码所示,导致轴上的文本不一致(见下图).
如何回到以前的行为(参见下图)或一致的字体方案?
编辑:
使用Latex后端我可以获得一个好的结果,但它非常慢.
无论如何,我认为内部后端应该能够获得一致的输出,并且切换到不同的后端并不是一个真正的解决方案,而是更多的解决方法.
使用乳胶后端:
#%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所示.
解决方法:
如果一致性是唯一的问题,则可以使用“Times”字体使用“Roman”样式.没有必要通过usetex使用Latex.而只需使用STIX字体集,Times字体和serif mathtext.
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()
内容总结
以上是互联网集市为您收集整理的python – Matplotlib 2不一致的字体全部内容,希望文章能够帮你解决python – Matplotlib 2不一致的字体所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。