【问题标题】:Import of Open Sans Font in matplotlibrc Ubuntu fails在 matplotlibrc Ubuntu 中导入 Open Sans 字体失败
【发布时间】:2014-07-07 01:18:09
【问题描述】:

我尝试导入字体Open Sans over:

import matplotlib as mpl
#update matplotlibrc
mpl.rcParams['font.family'] = 'Open Sans'

#testplot
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.title('Everything is crazy!!!', size=32)
plt.show()

如果我想绘图,matplotlib 找不到字体,给出如下错误消息:

/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Open Sans'] not found. Falling back to Bitstream Vera Sans
      (prop.get_family(), self.defaultFamily[fontext]))

/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1246: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=light:stretch=normal:size=medium. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmb10.ttf
      UserWarning)

字体安装在 Ubuntu 中,所有 OpenSans-*.ttf 文件也复制到 matplotlib 文件夹(在 Ubuntu 中为 /usr/share/matplotlib/mpl-data/fonts/ttf/)。 知道如何在 matplotlibrc 中使用字体吗?

【问题讨论】:

  • 是以下目录之一中存在的字体: /usr/X11R6/lib/X11/fonts/TTF/ /usr/X11/lib/X11/fonts /usr/share/fonts/ / usr/local/share/fonts/ /usr/lib/openoffice/share/fonts/truetype/
  • 我把它复制到 /usr/share/fonts/truetype 什么都没发生

标签: python ubuntu fonts matplotlib


【解决方案1】:

好的,这是一个老问题,但我刚刚安装了 Open Sans 以在 Matplotlib 中使用:

除了需要强制 Matplotlib 字体缓存重建外,OP 已遵循正确的步骤。最简单的方法:

from matplotlib import font_manager
font_manager._rebuild()

注意,除了在 Linux 上,以及上面讨论的字体路径,Matplotlib 似乎也在 ~/.fonts 中搜索,因此您可以在那里安装字体。

【讨论】:

    猜你喜欢
    • 2016-08-08
    • 2013-10-17
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2015-12-19
    • 2016-03-07
    相关资源
    最近更新 更多