Python 使用日志图键入1字体

Python 使用日志图键入1字体,python,matplotlib,Python,Matplotlib,我正在尝试使用Matplotlib图形作为照相机就绪的一部分 提交,出版社要求使用Type 1字体 只是 我发现PDF后端很乐意为用户输出Type-1字体 具有线性Y轴的简单图形,但为 对数轴 使用对数yscale需要使用mathtext,这似乎 使用类型3字体,可能是因为默认使用指数字体 符号我可以用一个丑陋的黑客绕过这个-使用 pyplot.yticks()强制轴刻度不使用指数,但 这需要移动打印区域以容纳较大的标签 (如10^6)或将轴写为10、100、1K等,使其适合 我已经用 matp

我正在尝试使用Matplotlib图形作为照相机就绪的一部分 提交,出版社要求使用Type 1字体 只是

我发现PDF后端很乐意为用户输出Type-1字体 具有线性Y轴的简单图形,但为 对数轴

使用对数yscale需要使用mathtext,这似乎 使用类型3字体,可能是因为默认使用指数字体 符号我可以用一个丑陋的黑客绕过这个-使用 pyplot.yticks()强制轴刻度不使用指数,但 这需要移动打印区域以容纳较大的标签 (如10^6)或将轴写为10、100、1K等,使其适合

我已经用 matplotlib主分支,以及1.1.1,它产生 同样的行为,所以我不知道这是一个bug,可能只是 意外的行为

#!/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型字体


谢谢

这是我用于照相机就绪提交的代码:

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")

通过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)
那么结果呢,

灵感来自: 和

为了提高认识,这也被发布在mpl用户邮件列表上:一些有用的参考资料(其中没有对这个问题的答案):&我只是用一个日志轴尝试了一下。这似乎对我有用。对不起,这个答复太晚了。希望你的论文投稿顺利!我也去过那里。