【问题标题】:How to change fonts in matplotlib (python)?如何更改 matplotlib(python)中的字体?
【发布时间】:2014-02-14 19:08:45
【问题描述】:

这听起来是一个简单的问题,但我没有找到任何有效的解决方案来更改在 python 中使用 matplotlib 制作的绘图中的字体(不是字体大小)。

我找到了几个教程,通过修改 matplotlib 存储其默认字体的文件夹中的一些文件来更改 matplotlib 的默认字体 - 请参阅 this blog post - 但我正在寻找一个不太激进的解决方案,因为我想使用我的绘图中不止一种字体(文本、标签、轴标签等)。

【问题讨论】:

  • 很高兴它有帮助:) 你能发布导致这个错误的代码吗?我自己没有看到这个错误,但这里有一些可能对你有帮助的链接。 matplotlib.1069221.n5.nabble.com/…matplotlib.1069221.n5.nabble.com/Fonts-not-found-td12936.html
  • 产生问题的代码是:hfont = {'fontname':'Helvetica'} plt.annotate('Country ', (0.17,0.95), xytext=None, xycoords='figure fraction',size=28, color='red', horizontalalignment = 'left', **hfont),错误是/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/m‌​atplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Helvetica'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext])),如果我在你的例子中使用Comic Sans MS作为字体名,代码可以工作。

标签: python fonts matplotlib


【解决方案1】:

我更喜欢雇用:

from matplotlib import rc
#rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('font',**{'family':'serif','serif':['Times']})
rc('text', usetex=True)

最后一行确保刻度标签的字体也正确。

【讨论】:

  • 全局更改字体的好方法,我一直在寻找这个。最后一行不是必需的,不确定 Tex 与此有什么关系。如果您因为 Matplotlib 找不到您想要的字体而出现错误,请查看此链接:scentellegher.github.io/visualization/2018/05/02/…
【解决方案2】:

您也可以使用rcParams 来全局更改字体系列。

 import matplotlib.pyplot as plt
 plt.rcParams["font.family"] = "cursive"
 # This will change to your computer's default cursive font

matplotlib 的字体族参数列表是here

【讨论】:

    【解决方案3】:

    Helvetica 字体不包含在 Windows 中,因此要使用它,您必须将其下载为 .ttf 文件。 然后你可以像这样引用matplotlib(用你的文件替换“crm10.ttf”):

    import os
    from matplotlib import font_manager as fm, rcParams
    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    
    fpath = os.path.join(rcParams["datapath"], "fonts/ttf/cmr10.ttf")
    prop = fm.FontProperties(fname=fpath)
    fname = os.path.split(fpath)[1]
    ax.set_title('This is a special font: {}'.format(fname), fontproperties=prop)
    ax.set_xlabel('This is the default font')
    
    plt.show()
    

    print(fpath) 会告诉你应该把 .ttf 放在哪里。

    你可以在这里看到输出: https://matplotlib.org/gallery/api/font_file.html

    【讨论】:

    • 有没有办法在全局范围内设置fontproperties,这样您就不必在每次调用时都指定它,例如set_title()set_xlabel() 等?
    • ^ 回答我自己的问题:stackoverflow.com/questions/35668219
    【解决方案4】:
    import pylab as plb
    plb.rcParams['font.size'] = 12
    

    import matplotlib.pyplot as mpl
    mpl.rcParams['font.size'] = 12
    

    【讨论】:

    • 令人难以置信的是,这正是我正在寻找的命令。绝对不是 OP 的要求
    • 提问者在问题中明确表示不打算更改字体大小。
    【解决方案5】:

    假设你想要 Comic Sans 作为标题,Helvetica 作为 x 标签。

    csfont = {'fontname':'Comic Sans MS'}
    hfont = {'fontname':'Helvetica'}
    
    plt.title('title',**csfont)
    plt.xlabel('xlabel', **hfont)
    plt.show()
    

    【讨论】:

    • 我试过了,效果很好!这正是我想要的。但是,对于某些字体,我有以下错误消息(不是全部):/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Helvetica'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))。如何在 matplotlib 已知的字体集中安装 Helvetica?
    • 找到 fontList.cache 文件,你只能使用那里列出的那些。或者看看这里stackoverflow.com/questions/20206906/…
    • 跟进上述评论。要查找您的 fontList.cache 文件,请使用 matplotlib.get_cachedir()
    • 谢谢!由于这只是使用 ** 语法解压缩字典,我尝试简单地执行 plt.title('title', fontname = 'monospace') 并且它也能正常工作!
    猜你喜欢
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    相关资源
    最近更新 更多