2017-02-15 30 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