python 带日志图表的Type 1字体

ddrv8njm  于 2023-02-11  发布在  Python
关注(0)|答案(3)|浏览(117)

我尝试使用Matplotlib图形作为照相机准备提交的一部分,出版社只要求使用Type 1字体。
我发现PDF后端很乐意为具有线性Y轴的简单图形输出Type-1字体,但为对数Y轴输出Type-3字体。
使用对数yscale会导致mathtext的使用,mathtext似乎使用Type3字体,大概是因为默认使用指数符号。()强制轴刻度不使用指数-但这需要移动绘图区域以容纳较大的标签(如10^6)或将坐标轴写成10、100、1K等,以使它们适合。
我已经用matplotlib master分支测试了下面的例子,以及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字体与日志轴?
谢谢!

0yg35tkg

0yg35tkg1#

这是我用于相机准备提交的代码:

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

4c8rllxm2#

通过matplotlib获得Type 1字体的首选方法似乎是使用TeX进行排版。这样做会导致 * all * 轴以默认数学字体排版,这通常是不希望的,但可以通过使用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/4189024http://wiki.scipy.org/Cookbook/Matplotlib/UsingTex

h43kikqp

h43kikqp3#

其他的答案对我来说都不起作用,但下面的答案起作用了:

import matplotlib.pylab as pylab

params = {'pdf.fonttype': 42}
pylab.rcParams.update(params)

相关问题