带日志图的Type 1字体 [英] Type 1 fonts with log graphs

查看:27
本文介绍了带日志图的Type 1字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use Matplotlib graphs as part of a camera-ready submission, and the publishing house requires the use of Type 1 fonts only.

我发现 PDF 后端愉快地输出 Type-1 字体带有线性 Y 轴的简单图形,但输出 Type-3 字体对数Y轴.

I'm finding that the PDF backend happily outputs Type-1 fonts for simple graphs with linear Y axes, but outputs Type-3 fonts for logarithmic Y axes.

使用对数yscale会导致使用数学文本,这似乎使用Type 3字体,大概是因为默认使用exponential符号.我可以使用一个丑陋的技巧来解决这个问题-使用pyplot.yticks()强制轴刻度不使用指数-但这将需要移动绘图区域以容纳较大的标签(例如10 ^ 6)或将轴写为10、100、1K等,以便它们适合.

Using a logarithmic yscale incurs the use of mathtext, which seems to use Type 3 fonts, presumably because of the default use of exponential notation. I can use an ugly hack to get around this - using pyplot.yticks() to force the axis ticks to not use exponents - but this would require moving the plot region to accommodate large labels (like 10 ^ 6) or writing the axes as 10, 100, 1K, etc. so they fit.

我已经用matplotlib 主分支截至今天,以及 1.1.1,产生相同的行为,所以我不知道这是一个错误,可能只是意外的行为.

I've tested the example below with the matplotlib master branch as of today, as well as 1.1.1, which produces the same behavior, so I don't know that this is a bug, probably just unexpected behavior.

#!/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 字体的干净方法吗?

Does anyone know a clean way to get Type 1 fonts with log axes?

谢谢!

推荐答案

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

This is the code I use for camera-ready submissions:

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

这篇关于带日志图的Type 1字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆