【问题标题】:How to resolve UserWarning: findfont: Could not match :family=Bitstream Vera Sans如何解决用户警告:findfont:无法匹配:family=Bitstream Vera Sans
【发布时间】:2013-09-19 16:41:42
【问题描述】:

关注this example

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
for i, label in enumerate(('A', 'B', 'C', 'D')):
    ax = fig.add_subplot(2,2,i+1)
    ax.text(0.05, 0.95, label, transform=ax.transAxes,
      fontsize=16, fontweight='bold', va='top')

plt.show()

我得到这个输出:

为什么我的标签重量正常,而文档显示这应该创建粗体字母ABCD

我也收到此警告:

Warning (from warnings module):
File "C:\Python27\lib\site-packages\matplotlib\font_manager.py", line 1228
UserWarning)
UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=italic:variant=normal:weight=bold:stretch=normal:size=x-small. Returning C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\Vera.ttf

OP 分辨率

【问题讨论】:

  • 根据 OPs 声明,我投票结束这个问题,因为这个问题不可重现。

标签: python-2.7 matplotlib


【解决方案1】:

我遇到了同样的问题,今天花了好几个小时。这是帮助我的解决方案:

import matplotlib
matplotlib.font_manager._rebuild()

有了这个,font_manager 可以轻松升级。

【讨论】:

    【解决方案2】:

    您问题中的示例适用于我的机器。因此,您肯定有图书馆问题。您是否考虑过使用乳胶制作粗体文字?这里是一个例子

    代码

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig, axs = plt.subplots(3, 1)
    ax0, ax1, ax2 = axs
    
    ax0.text(0.05, 0.95, 'example from question',
            transform=ax0.transAxes, fontsize=16, fontweight='bold', va='top')
    ax1.text(0.05, 0.8, 'you can try \\textbf{this} using \\LaTeX', usetex=True,
            transform=ax1.transAxes, fontsize=16, va='top')
    ax2.text(0.05, 0.95,
             'or $\\bf{this}$ (latex math mode with things like '
             '$x_\mathrm{test}^2$)',
            transform=ax2.transAxes, fontsize=10, va='top')
    
    plt.show()
    

    【讨论】:

      【解决方案3】:

      尝试使用weight 而不是fontweight

      【讨论】:

        【解决方案4】:

        也许可以试试这个 -

        plt.rcParams['axes.labelsize'] = 16
        plt.rcParams['axes.labelweight'] = 'bold'
        

        在您的程序中全局执行此操作。

        【讨论】:

          【解决方案5】:

          不确定您是否仍有问题。我在 Python 2.7 的 Anaconda/Spyder 中尝试了您的代码。这些图带有粗体标签(A、B、C、D)。我同意问题可能与图书馆有关。尝试替换/更新 font_manager.py 或确认字体文件存在:

          Lib\site-packages\matplotlib\mpl-data\fonts\ttf\

          【讨论】:

            猜你喜欢
            • 2020-08-05
            • 2022-08-05
            • 2018-06-23
            • 2020-08-01
            • 2014-10-07
            • 1970-01-01
            • 2013-04-29
            • 1970-01-01
            • 2013-09-05
            相关资源
            最近更新 更多