2017-06-01 46 views
1

我被要求更改我的论文中使用Type 3字体的一些数字以使用Type 1字体,并且我发现instruction 做这个工作。Matplotlib:从类型3字体更改为类型1字体后.pdf中的非统一字体

加入以下3行,我能够产生一个Type 1字体.pdf文件:

matplotlib.rcParams['ps.useafm'] = True 
matplotlib.rcParams['pdf.use14corefonts'] = True 
matplotlib.rcParams['text.usetex'] = True 

然而,当X-TICK和y蜱对我的老情节的字体是一致的,我添加了3行代码后,它们的字体变得不同了。这对我来说似乎很奇怪,因为这种变化只适用于其中一个蜱。

enter image description here

这里是脚本在python:

import matplotlib 
matplotlib.use('PDF') 
import matplotlib.pyplot as plt 
from matplotlib.backends.backend_pdf import PdfPages 


# Switch to Type 1 Fonts. 
matplotlib.rcParams['ps.useafm'] = True 
matplotlib.rcParams['pdf.use14corefonts'] = True 
matplotlib.rcParams['text.usetex'] = True 


def plot_pdf_figure(path, title, xl, yl, xt_list, yl_list, style_list): 
    fig = plt.figure(path) 

    ax = fig.add_subplot(111) 
    ax.set_title(title) 
    ax.set_xlabel(xl, style='italic') 
    ax.set_ylabel(yl) 

    x_list = range(0, len(xt_list)) 
    plt.xticks(x_list, xt_list) 
    for y_list, style in zip(yl_list, style_list): 
     ax.plot(x_list, y_list, style) 

    pp = PdfPages(path) 
    pp.savefig(fig) 
    pp.close() 



plot_pdf_figure("test_plot.pdf", "test", "x-axis", "y-axis", [1,2,3,4,5,6], [[1,2,4,8,16,32],[32,16,8,4,2,1]], ["-ro","-gs"]) 

谢谢你的任何指导或帮助!

+0

它看起来并不像你实际上对rcParams进行更改。尝试在该块之前和之后删除'''''? – farenorth

+0

这是为了比较。当前的代码将输出Type 3 Font,但删除'''将输出Type 1 Font,并且可以观察字体差异。如果不清楚,我会删除三个撇号。 – Sean

回答

3

这是您的plt.xticks调用的结果。

rcParams['text.usetex']True时,MPL在$中打包标记。例如,ax.get_xticklabels()[0].get_text()将在usetexFalse时给出u'0',并且当usetexTrue时为u'$0$'

但是,当您覆盖默认的刻度标签时,您必须自己包装它们,否则您将获得sans-serif字体。因此,要解决这个问题,我想你plt.xticks行更改为类似:

plt.xticks(x_list, ['$' + str(xt) + '$' for xt in xt_list]) 

这里我使用列表解析来遍历所有的ticklabels。我想也可以将默认的sans-serif字体族更改为与serif族相同的(serif)字体,但我认为这可能不能完全解决x和y标签之间的差异,因为字符串是实际上不同。