Matplotlib PDF导出使用错误的字体 [英] Matplotlib PDF export uses wrong font

查看:266
本文介绍了Matplotlib PDF导出使用错误的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为演示文稿生成高质量的图表.我正在使用Python的matplotlib生成图形.不幸的是,PDF导出似乎忽略了我的字体设置.

I want to generate high-quality diagrams for a presentation. I’m using Python’s matplotlib to generate the graphics. Unfortunately, the PDF export seems to ignore my font settings.

我尝试通过将FontProperties对象传递给文本绘图函数并通过全局设置选项来设置字体.作为记录,以下是MWE来重现该问题:

I tried setting the font both by passing a FontProperties object to the text drawing functions and by setting the option globally. For the record, here is a MWE to reproduce the problem:

import scipy
import matplotlib
matplotlib.use('cairo')
import matplotlib.pylab as pylab
import matplotlib.font_manager as fm

data = scipy.arange(5)

for font in ['Helvetica', 'Gill Sans']:
    fig = pylab.figure()
    ax = fig.add_subplot(111)
    ax.bar(data, data)
    ax.set_xticks(data)
    ax.set_xticklabels(data, fontproperties = fm.FontProperties(family = font))
    pylab.savefig('foo-%s.pdf' % font)

在两种情况下,产生的输出都是相同的,并且使用Helvetica(是的,我 do 都安装了两种字体).

In both cases, the produced output is identical and uses Helvetica (and yes, I do have both fonts installed).

可以肯定的是,以下内容也无济于事:

Just to be sure, the following doesn’t help either:

matplotlib.rc('font', family = 'Gill Sans')

最后,如果我替换了后端,而不是使用本机查看器:

Finally, if I replace the backend, instead using the native viewer:

matplotlib.use('MacOSX')

可以显示正确的字体,但只能在查看器GUI中显示. PDF输出再次错误.

I do get the correct font displayed – but only in the viewer GUI. The PDF output is once again wrong.

请确保–我可以设置其他字体–但只能选择其他字体家族类别:我可以设置serif字体或fantasymonospace.但是所有的sans-serif字体似乎都默认为Helvetica.

To be sure – I can set other fonts – but only other classes of font families: I can set serif fonts or fantasy or monospace. But all sans-serif fonts seem to default to Helvetica.

推荐答案

"family"参数和相应的rc参数并非旨在指定字体名称实际上可以通过这种方式使用.有一个(可以说是巴洛克式的)类似CSS的字体选择系统,它可以帮助同一脚本在不同的计算机上工作,选择最接近的可用字体.通常推荐的使用方式,例如Gill Sans将其添加到rc参数font.sans-serif的值的前面(请参见

The "family" argument and the corresponding rc parameter are not meant to specify the name of the font can actually be used this way. There's an (arguably baroque) CSS-like font selection system that helps the same script work on different computers, selecting the closest font available. The usually recommended way to use e.g. Gill Sans is to add it to the front of the value of the rc parameter font.sans-serif (see sample rc file), and then set font.family to sans-serif.

如果字体管理器出于某种晦涩的原因认为Gill Sans不是最接近您的规范的匹配项,这可能会很烦人.绕过字体选择逻辑的一种方法是使用FontProperties(fname='/path/to/font.ttf')( docstring ).

This can be annoying if the font manager decides for some obscure reason that Gill Sans is not the closest match to your specification. A way to bypass the font selection logic is to use FontProperties(fname='/path/to/font.ttf') (docstring).

在您的情况下,我怀疑MacOSX后端通过操作系统的机制使用字体,因此自动支持各种字体,但是pdf后端具有其自己的字体支持代码,该代码不支持您的Gill Sans版本.

In your case, I suspect that the MacOSX backend uses fonts via the operating system's mechanisms and so automatically supports all kinds of fonts, but the pdf backend has its own font support code that doesn't support your version of Gill Sans.

这篇关于Matplotlib PDF导出使用错误的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆