13

カメラ対応の提出物の一部として Matplotlib グラフを使用しようとしていますが、出版社は Type 1 フォントのみの使用を要求しています。

PDF バックエンドは、Y 軸が線形の単純なグラフには Type-1 フォントを出力しますが、対数 Y 軸には Type-3 フォントを出力します。

対数 yscale を使用すると、おそらく指数表記がデフォルトで使用されているため、Type 3 フォントを使用しているように見える数学テキストが使用されます。私はこれを回避するために醜いハックを使用することができます. 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)

対数軸で Type 1 フォントを取得するクリーンな方法を知っている人はいますか?

ありがとう!

4

2 に答える 2

8

これは、カメラ対応の送信に使用するコードです。

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")
于 2013-08-31T20:39:42.473 に答える
4

matplotlib を介して Type 1 フォントを取得するための推奨される方法は、組版に TeX を使用することです。これを行うと、すべての軸がデフォルトの数学フォントでタイプセットされます。これは通常は望ましくありませんが、TeX コマンドを使用することで回避できます。

簡単に言えば、私はこの解決策を見つけました:

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/4189024および http://wiki.scipy.org/Cookbook/Matplotlib/UsingTex

于 2015-05-18T17:20:19.210 に答える