簡體   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