Skip to main content
 首页 » 编程设计

python之为什么 Matplotlib 的 PdfPages 将选项卡打印为框(字体编码)

2024年10月01日7leader

当我运行 Matplotlib's multi page example 的修改版本时在标题中添加了一个选项卡后,我得到以下输出:

这是我的工作示例。代码上面的注释是我找到的建议here在Non-ASCII characters in Matplotlib , 但到目前为止没有成功。

# -*- coding: utf-8 -*- 
import matplotlib 
from matplotlib.backends.backend_pdf import PdfPages 
from pylab import * 
 
#matplotlib.rc('font', family='DejaVu Sans') 
 
#matplotlib.rc('font', **{'sans-serif' : 'Arial', 
#                           'family' : 'sans-serif'}) 
 
#matplotlib.rcParams['pdf.fonttype'] = 42 
#matplotlib.rcParams['ps.fonttype'] = 42 
 
pdf = PdfPages('multipage_pdf.pdf') 
 
figure(figsize=(3,3)) 
plot(range(7), [3,1,4,1,5,9,2], 'r-o') 
title('Page\tOne') 
savefig(pdf, format='pdf') # note the format='pdf' argument! 
close() 
 
pdf.close() 

有什么解决办法吗?

请您参考如下方法:

解决方法是添加

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

如前所述here和 here .

我找不到哪一行有魔力,在我的情况下似乎不需要所有的,但我只是添加了所有。如果 tex 提示无法对某些内容进行编码有问题,您可以评论 ['text.usetex'] 它仍然有效。

来自 matplotlib 文档:

Add "pdf.use14corefonts: True" in your configuration file to use only the 14 PDF core fonts. These fonts do not need to be embedded; every PDF viewing application is required to have them. This results in very light PDF files you can use directly in LaTeX or ConTeXt documents generated with pdfTeX, without any conversion.

These fonts are: Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique, Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique, Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Symbol, ZapfDingbats.