更改Matplotlib的默认字体

32

我正在尝试将matplotlib的默认字体更改为Helvetica Neue。在我的Mac上使用EPD/Canopy一段时间前一切正常。

现在在Ubuntu上尝试做同样的事情,但不起作用。

这是我所做的:

  1. 安装了Helvetica Neue

  2. $ fc-match 'Helvetica Neue':Light
    HelveticaNeue-Light.otf: "Helvetica Neue" "細體"
    
  3. 将odt/dfont转换为ttf:

    fondu -show HelveticaNeue.dfont
    
  4. 将matplotlibrc更改为

    $ cat ~/.config/matplotlib/matplotlibrc
    ...
    font.family: Helvetica Neue
    

    我也尝试了以下方式:

    font.family: sans-serif
    font.sans-serif: Helvetica Neue
    
  5. 我清除了字体缓存

    rm ~/.config/matplotlib/fontList.cache
    
    但是这些步骤对我都没有起作用。
        $ python -c 'from  matplotlib import pyplot as plt; plt.plot(1); plt.savefig("/tmp/test.png")'
        /usr/local/lib/python2.7/dist-packages/matplotlib-1.3.0-py2.7-linux-x86_64.egg/matplotlib/font_manager.py:1236: 
        UserWarning: findfont: Font family ['Helvetica Neue'] not found. Falling back to Bitstream Vera Sans
    

    (prop.get_family(), self.defaultFamily[fontext]))

    版本是 1.3.0

        $ python -c 'import matplotlib; print  matplotlib.__version__'
        1.3.0
    

    我也尝试将字体移动到~/.config/matplotlib/fonts/ttf,但没有成功。


    编辑: 按建议,我尝试为特定的文本选择特定的字体。

    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt
    import matplotlib.font_manager as font_manager
    
    path = '/home/<myusername>/.fonts/HelveticaNeue.ttf'
    
    prop = font_manager.FontProperties(fname=path)
    prop.set_weight = 'light'
    mpl.rcParams['font.family'] = prop.get_name()
    mpl.rcParams['font.weight'] = 'light'
    
    fig, ax = plt.subplots()
    ax.set_title('Text in a cool font', fontproperties=prop, size=40)
    plt.savefig('/tmp/test2.png')
    

    但这没有任何区别。

    /usr/local/lib/python2.7/dist-packages/matplotlib-1.3.0-py2.7-linux-x86_64.egg/matplotlib/font_manager.py:1236: 
    UserWarning: findfont: Font family ['Helvetica Neue'] not found. Falling back to Bitstream Vera Sans
    

    然而,我似乎只在使用Helvetica/Helvetica Neue字体时遇到了这个问题。(prop.get_family(), self.defaultFamily[fontext]))


1
在尝试更改默认设置之前,您是否能够直接/明确地使用新字体?https://dev59.com/KWMk5IYBdhLWcg3w8iaC#18821968展示了列出python/mpl可用字体的一种方法,您可以尝试使用`plt.text(1, 1, 'rattling', font_family="Helvetica Neue")`。另请参见https://dev59.com/tGQn5IYBdhLWcg3wxZni#16574948。 - Bonlenfum
很好的观点。我已经尝试过了,但没有成功。我已相应地编辑了问题。 - gozzilli
你能试试从主分支安装吗?最近有很多与字体查找相关的工作。而且看起来这个字体的名称中有非ASCII字符,如果我没记错的话,上游某处可能存在一个导致它无法正常工作的错误。 - tacaswell
@tcaswell 我尝试了主分支('1.4.x'),结果一样。请注意,相同的字体(完全相同,我复制到另一台MacOS机器上)在我的MacOS机器上可以正常工作。 - gozzilli
1
请在 GitHub 上创建一个问题(issue)来说明这个问题。我觉得这很像一个 Bug。 - tacaswell
也许你还需要删除 fontList.cache 文件?这就是我所做的。 - mcmayer
4个回答

17
这不会永久改变你的字体,但值得一试。
matplotlib.rc('font', family='sans-serif') 
matplotlib.rc('font', serif='Helvetica Neue') 
matplotlib.rc('text', usetex='false') 
matplotlib.rcParams.update({'font.size': 22})

1
我认为问题不在于默认行为,而更多地在于它无法识别我的Helvetica Neue字体。我在Github上提交了一个错误报告。 - gozzilli
我正在使用EPD Python版本(https://www.enthought.com/products/epd/),并且Helvetica Neue在其中被识别。 - Brian
1
太好了,谢谢。在哪个操作系统上?在我的EPD(实际上是Canopy)上的Mac也可以工作,但在Ubuntu上不起作用。 - gozzilli
我使用的是Ubuntu 12.04。希望这可以帮到你。 - Brian

10

Ubuntu 14.04 LTS

上传字体文件

sudo cp NotoSansKR-Regular.otf /usr/share/fonts/
更新字体缓存。
sudo fc-cache -fv

您可以查看字体列表

fc-list

重新启动ipython等,检查字体列表

[f.name for f in matplotlib.font_manager.fontManager.ttflist]

选择你的字体名称

import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['font.family'] = 'Noto Sans Korean'

绘画

plt.title(u'한글 제목')
plt.xlabel(u'한글 축 이름')
plt.plot(range(5))

1
看起来不错,但是不能工作:[f.name for f in matplotlib.font_manager.fontManager.ttflist] 报错 AttributeError: 'module' object has no attribute 'font_manager' - imrek
2
你能试一下这个命令吗?import matplotlib.font_manager - KyungHoon Kim
2
有时候需要运行matplotlib.font_manager._rebuild()才能使用新字体。 - Tobias
@Tobias 当我尝试这样做时,我会得到AttributeError:模块“matplotlib.font_manager”没有属性“_rebuild” - Rylan Schaeffer

2
金已经介绍了动态解决方案的完美运作,这里有两种静态方式可以做到同样的效果。
首先,您可以在Matplotlib的rc文件中添加一行。有关定位该文件和详细设置的更多信息,请参阅此页面
font.family : NanumGothic

其次,如果你正在使用ipython,你可以将用于设置字体的一些命令放入交互式shell配置文件中。查找名为ipython_config.py的文件,该文件通常位于~/.ipython/somewhere下。然后在列表中添加两行,c.InteractiveShellApp.exec_lines。

c.InteractiveShellApp.exec_lines = [
    "import matplotlib as mpl",
    "mpl.rcParams['font.family'] = 'NanumGothic'"
]

前者始终在任何你运行shell脚本的环境中起作用,因为它在导入matplotlib时加载字体。


1
我的字体缓存出现在不同的位置(.cache/matplotlib/fontList.cache)。之前我在不同的地方有三个 :/。也许尝试在您的主目录中搜索它:
find ~/ -name fontList.cache -exec rm {} \;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接