use*_*469 11 python matplotlib
我正在尝试使用Matplotlib图形作为相机就绪提交的一部分,并且出版社只需要使用Type 1字体.
我发现PDF后端很乐意为具有线性Y轴的简单图形输出Type-1字体,但是输出用于对数Y轴的Type-3字体.
使用对数yscale会导致使用mathtext,它似乎使用Type 3字体,可能是因为默认使用指数表示法.我可以使用一个丑陋的黑客来解决这个问题 - 使用pyplot.yticks()强制轴刻度不使用指数 - 但这需要移动绘图区域以容纳大标签(如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)
Run Code Online (Sandbox Code Playgroud)
有没有人知道用日志轴获得Type 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)
Run Code Online (Sandbox Code Playgroud)
在运行之前,字体不会出现 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")
Run Code Online (Sandbox Code Playgroud)
通过 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)
Run Code Online (Sandbox Code Playgroud)
然后结果是这样的
灵感来自: /sf/answers/1449640461/和 http://wiki.scipy.org/Cookbook/Matplotlib/UsingTex