Matplotlib 2字体不一致 [英] Matplotlib 2 inconsistent font

查看:48
本文介绍了Matplotlib 2字体不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Anaconda Python更新到了最新版本(4.3),在那里他们将Matplotlib升级到了版本2.

升级对默认样式进行了一些重大更改(

如何恢复到以前的行为(见下图)或一致的字体方案?

编辑:使用Latex后端,我可以获得不错的结果,但是速度非常慢.无论如何,我认为内部后端应该能够获得一致的输出,切换到不同的后端并不是真正的解决方案,而是一种解决方法.

要使用乳胶后端,请执行以下操作:

 #%matplotlib内联#%matplotlib笔记本#%config InlineBackend.figure_format = 'svg'导入scipy作为sc导入matplotlib.pyplot作为plt导入 matplotlib# http://matplotlib.org/users/dflt_style_changes.htmlparams = {'legend.fontsize':18,'axes.labelsize':18,'axes.titlesize': 18,'xtick.labelsize':12,'ytick.labelsize': 12,'mathtext.fontset':'cm','mathtext.rm': '衬线','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无花果= plt.figure('Fig')ax = fig.add_subplot(1, 1, 1)线= 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所示.

解决方案

在试图找到我的问题的解决方案时,我尝试比较新旧 rcParams 的字典并设置与 mathtext 字体不同且相关的元素:效果很好.

代码是:

#%matplotlib 内联#%matplotlib 笔记本#%config InlineBackend.figure_format = 'svg'将 scipy 作为 sc 导入导入matplotlib.pyplot作为plt导入 matplotlib#http://matplotlib.org/users/dflt_style_changes.html参数 = {'legend.fontsize': 18,'axes.labelsize': 18,'axes.titlesize':18,'xtick.labelsize':12,'ytick.labelsize':12,'mathtext.fontset':'cm','mathtext.rm': '衬线','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无花果= plt.figure('Fig')ax = fig.add_subplot(1, 1, 1)线= 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',

其结果是:

我认为在Latex文档中非常好且一致.

@ImportanceOfBeingErnest 在此线程中的另一个 答案 也很简洁.

I updated Anaconda Python to the latest version (4.3), where they upgraded Matplotlib to version 2.

The upgrade has made some major changes to the default style (see here). And, while I really like some of those changes, I am not in agreement with a few of them.

Hence I did some modifications, as suggested in the link above:

#%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')

Using Latex as the axes labels, as in the code above, results in a figure with inconsistent text on axes (see the following image).

How to get back to the previous behaviour (see the image below) or to a consistent font scheme?

EDIT: Using the Latex back-end I am able to get a good result, but it is extremely slow. Anyway, I think the internal back-end should be able to get a consistent output and switching to a different back-end is not a real solution, but more a workaround.

To use the latex back-end:

#%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')

The result with matplotlib 2 is:

The resulting plot with the older version is (still a bit different, maybe due to some latex differences):

But again, the desired result is what obtained from an older version of matplotlib and in displayed in figure 2.

解决方案

While trying to find a solution to my question, I tried comparing the dictionaries of the old and new rcParams and setting the elements which were different and related to mathtext font: the result is quite good.

The code is:

#%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')

hence adding also:

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

which results in:

that I consider quite good and consistent in a Latex document.

The other answer in this thread from @ImportanceOfBeingErnest is also neat and nice.

这篇关于Matplotlib 2字体不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆