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

标签 python pdf matplotlib character-encoding pdf-generation

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

enter image description here

这是我的工作示例。代码上面的注释是我找到的建议hereNon-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

如前所述herehere .

我找不到哪一行有魔力,在我的情况下似乎不需要所有的,但我只是添加了所有。如果 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.

关于python - 为什么 Matplotlib 的 PdfPages 将选项卡打印为框(字体编码)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47346635/

相关文章:

python - BeautifulSoup 中 find_all 之后按标签过滤

javascript - 使用 javascript 获取 PDF 的设备 ID

ruby-on-rails - send_file pdf时将浏览器标签标题更改为文件名

ios - 有没有办法将pdf文件嵌入到html5页面中?

python - 如何在 matplotlib 中将 RGBA 图像叠加在 RGB 图像上

python - OpenAL Python openal.audio 模块未找到

python - 实时从子进程中读取标准输出

python - Matplotlib 中 Facecolors 使用的标准化颜色图

python - 为每个级别使用不同的格式分割 matplotlib 标题

python - 获取JSON数据的特定值