更改 Matplotlib 的默认字体 [英] Change Matplotlib's default font

查看:63
本文介绍了更改 Matplotlib 的默认字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to change matplotlib's default font to Helvetica Neue. On my Mac with EPD/Canopy everything worked fine some time ago.

现在尝试在 ubuntu 上做同样的事情,但它不起作用.

Trying to do the same on ubuntu now and it's not working.

这就是我所做的:

  1. 安装 Helvetica Neue

  1. Installed Helvetica Neue

$ fc-match 'Helvetica Neue':Light
HelveticaNeue-Light.otf: "Helvetica Neue" "細體"

  • 将 odt/dfont 转换成 ttf:

  • Converted the odt/dfont into ttf:

    fondu -show HelveticaNeue.dfont
    

  • 将 matplotlibrc 更改为

  • changed matplotlibrc to

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

    我也试过:

    font.family: sans-serif
    font.sans-serif: Helvetica Neue
    

  • 我删除了字体缓存

  • I removed the font cache

    rm ~/.config/matplotlib/fontList.cache
    

  • 但是这些步骤都不适合我.

    But none of these steps are working for me.

        $ 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]))

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

    版本是 1.3.0

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

    我也尝试将字体移动到 ~/.config/matplotlib/fonts/ttf 但它没有用.

    I also tried moving the fonts to ~/.config/matplotlib/fonts/ttf but it didn't work.

    按照建议,我尝试为特定文本选择特定字体.

    As suggested I tried selecting a specific font for a specific text.

    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]))

    However I seem to experience this problem only with this Helvetica/Helvetica Neue font. (prop.get_family(), self.defaultFamily[fontext]))

    推荐答案

    这不会永久更改您的字体,但值得一试.

    This will not change your font permanently, but it's worth a try.

    matplotlib.rc('font', family='sans-serif') 
    matplotlib.rc('font', serif='Helvetica Neue') 
    matplotlib.rc('text', usetex='false') 
    matplotlib.rcParams.update({'font.size': 22})
    

    这篇关于更改 Matplotlib 的默认字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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