2012-10-30 12 views
9

私はMatplotlibグラフをカメラ対応の 提出物の一部として使用しようとしており、出版社はタイプ1のフォント のみを使用する必要があります。タイプ1のフォントとロググラフ

私は、PDFのバックエンドが のリニアY軸を持つ単純グラフのType-1フォントをうまく出力しますが、 対数Y軸のType-3フォントを出力していることがわかりました。対数YSCALEを使用して

はおそらく指数 表記のデフォルトの使用の 使用Type 3フォントには思えるmathtextの使用を、発生します。 pyplot.yticks()を使用して指数を使用しないようにするには、 を使用してください。 これは、大きなラベル (10^6のような)または書き込みを行うようにプロット領域を移動する必要があります軸は10,100,1Kなどとなり、フィットします。

は私が 同じ動作生成 matplotlibのマスター、今日のように枝だけで​​なく、1.1.1、して以下に例をテストしてみたので、これはおそらく、ちょうど バグであることを私は知りません予期せぬ動作。

#!/usr/bin/env python 
# Simple program to test for type 1 fonts. 
# Generate a line graph w/linear and log Y axes. 

from matplotlib import rc, rcParams 

rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) 
#rc('font',**{'family':'sans-serif','sans-serif':['computer modern sans serif']}) 

# These lines are needed to get type-1 results: 
# http://nerdjusttyped.blogspot.com/2010/07/type-1-fonts-and-matplotlib-figures.html 
rcParams['ps.useafm'] = True 
rcParams['pdf.use14corefonts'] = True 
rcParams['text.usetex'] = False 

import matplotlib.pyplot as plt 

YSCALES = ['linear', 'log'] 

def plot(filename, yscale): 
    plt.figure(1) 
    xvals = range(1, 2) 
    yvals = xvals 
    plt.plot(xvals, yvals) 
    plt.yscale(yscale) 
    plt.savefig(filename + '.pdf') 

if __name__ == '__main__': 
    for yscale in YSCALES: 
     plot('linegraph-' + yscale, yscale) 

ログ軸を持つタイプ1フォントを取得するクリーンな方法を知っている人はいますか?

ありがとうございます!

+0

、これも上に投稿されましたmpl-usersメーリングリスト:http://matplotlib.1069221.n5.nabble.com/Type-1-fonts-with-log-graphs-tt39606.html – pelson

+0

S ome有用な参考文献(それらの中にこの質問に対する答えはありません):http://matplotlib.1069221.n5.nabble.com/Type-1-font-infigure-needed-td10294.html&http://nerdjusttyped.blogspot .co.uk/2010/07/type-1-fonts-and-matplotlib-figures.html – pelson

答えて

6

これは私がカメラレディ提出に使用するコードです:

from matplotlib import pyplot as plt 

def SetPlotRC(): 
    #If fonttype = 1 doesn't work with LaTeX, try fonttype 42. 
    plt.rc('pdf',fonttype = 1) 
    plt.rc('ps',fonttype = 1) 

def ApplyFont(ax): 

    ticks = ax.get_xticklabels() + ax.get_yticklabels() 

    text_size = 14.0 

    for t in ticks: 
     t.set_fontname('Times New Roman') 
     t.set_fontsize(text_size) 

    txt = ax.get_xlabel() 
    txt_obj = ax.set_xlabel(txt) 
    txt_obj.set_fontname('Times New Roman') 
    txt_obj.set_fontsize(text_size) 

    txt = ax.get_ylabel() 
    txt_obj = ax.set_ylabel(txt) 
    txt_obj.set_fontname('Times New Roman') 
    txt_obj.set_fontsize(text_size) 

    txt = ax.get_title() 
    txt_obj = ax.set_title(txt) 
    txt_obj.set_fontname('Times New Roman') 
    txt_obj.set_fontsize(text_size) 

あなたはsavefig

を実行するまでフォントが表示されません。例:

import numpy as np 

SetPlotRC() 

t = np.arange(0, 2*np.pi, 0.01) 
y = np.sin(t) 

plt.plot(t,y) 
plt.xlabel("Time") 
plt.ylabel("Signal") 
plt.title("Sine Wave") 

ApplyFont(plt.gca()) 
plt.savefig("sine.pdf") 
+1

私はログ軸を使って試しました。それは私のために働くようです。申し訳ありませんが、この回答はとても遅いです。あなたの論文提出がうまくいったことを願っています!私もそこにいました。 – DrRobotNinja

4

好ましい方法へmatplotlibでタイプ1のフォントを取得するには、タイプセットにTeXを使用するようです。そうすると、のすべての軸がデフォルトの算術フォントにタイプセットされます。これは通常は望ましくありませんが、TeXコマンドを使用することで回避できます。

かいつまんで、私はこの解決策を見つけた:その後に触発され、この resulting image

になり

import matplotlib.pyplot as mp 
import numpy as np 

mp.rcParams['text.usetex'] = True #Let TeX do the typsetting 
mp.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}', r'\sansmath'] #Force sans-serif math mode (for axes labels) 
mp.rcParams['font.family'] = 'sans-serif' # ... for regular text 
mp.rcParams['font.sans-serif'] = 'Helvetica, Avant Garde, Computer Modern Sans serif' # Choose a nice font here 

fig = mp.figure() 
dim = [0.1, 0.1, 0.8, 0.8] 

ax = fig.add_axes(dim) 
ax.text(0.001, 0.1, 'Sample Text') 
ax.set_xlim(10**-4, 10**0) 
ax.set_ylim(10**-2, 10**2) 
ax.set_xscale("log") 
ax.set_yscale("log") 
ax.set_xlabel('$\mu_0$ (mA)') 
ax.set_ylabel('R (m)') 
t = np.arange(10**-4, 10**0, 10**-4) 
y = 10*t 

mp.plot(t,y) 

mp.savefig('tmp.png', dpi=300) 

:ちょうど意識のため https://stackoverflow.com/a/20709149/4189024http://wiki.scipy.org/Cookbook/Matplotlib/UsingTex

関連する問題