繁体   English   中英

Matplotlib:从Type 3字体更改为Type 1字体后,.pdf中的字体不一致

[英]Matplotlib: Non-uniform fonts in .pdf after changing from Type 3 Font to Type 1 Font

有人问我,改变我的论文的一些数字使用类型3字体使用Type 1字体,我发现的指令做只是工作。

通过添加以下3行,我可以生成Type 1 Font .pdf文件:

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

但是,虽然我的旧图上的x-tick和y-tick字体是统一的,但是当我添加三行代码后,它们的字体以某种方式变得不同。 这对我来说似乎很奇怪,因为更改仅适用于一个价格变动。

在此处输入图片说明

这是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"])

感谢您的指导或帮助!

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

rcParams['text.usetex']True ,MPL将ticklabel包装在$ 例如,当usetexFalse时, ax.get_xticklabels()[0].get_text()将给出u'0' ,而当usetexTrueusetex u'$0$'

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

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

在这里,我使用列表推导遍历ticklabel。 我想也可以将默认的sans-serif字体系列更改为与您的衬线字体相同的字体,但是我认为这可能无法完全解决x和y标签之间的差异。实际上不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM