2017-02-15 8 views
6
私は、彼らはアップグレードがデフォルトのスタイル( see here)にいくつかの主要な変更を加えたバージョン2

matplotlibの2一貫性のないフォント

にmatplotlibのをアップグレードし、最新バージョン(4.3)、へアナコンダのPythonを更新

。 そして、私はそれらの変更のいくつかが本当に好きですが、私はそれらの変更のいくつかと一致していません。

したがって、上記のリンクに示唆したように、私は、いくつかの変更をした:

軸ラベルとしてラテックスを使用
#%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

EDIT: ラテックスバックエンドを使用して、私は良い結果を得ることができていますが、それは非常に遅いです。 とにかく、内部のバックエンドは一貫した出力を得ることができ、別のバックエンドに切り替えることは本当の解決策ではなく、より回避策であると思います。

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

しかし、再び、望ましい結果は、あなたが提供したリンクからは、図2

+0

組み込み数学レンダリングエンジン(mathtext)を使用するときのデフォルトの数学フォントが「コンピュータモダン」(LaTeX風)から「DejaVu Sans」に変更されました。言い換えれば、_default_ビヘイビアは、下のグラフのようなサンセリフフォントでなければなりません。 '' 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', 

:私はLaTeX文書でかなり良いと一貫性の考慮

enter image description here

他のanswer in this thread from @ImportanceOfBeingErnestもすっきりとしています。

1

に表示さmatplotlibののとでは、旧バージョンから取得したものです。この行に

matplotlib.style.use('classic') 
を追加

A ‘classic’ style sheet is provided so reverting to the 1.x default values is a single line of python

mpl.style.use('classic')

スクリプトに

があなたの問題を解決するはずです。

python2.7/matplotlib 2でテストしたところ、うまく動作しました(つまり、matplotlib 1.xのフォントを取得しました)。

+0

はい、本当に古いスタイルです。 2番目のプロットと同じスタイルは得られません。 私は新しいスタイルを保ち、フォントの問題を修正したいと思います。 – Alex

+0

@Alex:あなたは軸の目盛だけを意味すると思った。フォントの詳細については、http://matplotlib.org/users/mathtext.html#fontsを参照してください。あなたのコードを '' mathtext.fontset ':' stixsans '、 '' and/or' 'mathtext.rm': 'sansserif' 'に変更すると、かなり良い結果が得られます。 – Eiffel

+0

しかしラベルのラテックスは違ったフォントを持っています。これは醜いものです。 – Alex

4

一貫性が唯一の問題である場合は、 "Times"フォントを使用して "Roman"スタイルを使用できます。ラテックスをusetexで使用する必要はありません。代わりに、単に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() 

enter image description here

+0

きちんとして、出力は素晴らしく一貫しています。その間、私はrcParamの辞書を比較しようとしました。私はさまざまな要素を設定しようとしました。結果はかなり良いです。私は自分自身に可能な答えとしてこれを入れようとします(自分の答えを受け入れることなく、私は決定的であると感じません)。 – Alex

関連する問題