返回

python-matplotlib 在哪里找到字体以及如何向其中添加新字体?

发布时间:2022-08-12 11:48:49 429
# node.js

首先,我把我的自定义字体STKAITI.TTF/usr/local/share/fonts,然后通过重建字体缓存fc-cache -fv.

但我仍然不能在matplotlib中使用它。

In [13]: matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
Out[13]:
['/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf',
 '/usr/local/share/fonts/STKAITI.TTF', # font is here
 '/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf',
 '/usr/local/share/fonts/NotoSansSC-Regular.otf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf']

然后:

import matplotlib.pyplot as plt
In [6]: plt.rcParams["font.sans-serif"] ## this is definded in my '~/.config/matplotlib/matplotlibrc'
Out[6]:
['STKaiti', # is here
 'DejaVu Sans',
 'Bitstream Vera Sans',
 'Computer Modern Sans Serif',
 'Lucida Grande',
 'Verdana',
 'Geneva',
 'Lucid',
 'Arial',
 'Helvetica',
 'Avant Garde',
 'sans-serif']

好的...测试

In [7]: matplotlib.font_manager.get_fontconfig_fonts()
:1: MatplotlibDeprecationWarning:
The get_fontconfig_fonts function was deprecated in Matplotlib 3.5 and will be removed two minor releases later.
  matplotlib.font_manager.get_fontconfig_fonts()
Out[7]:
['/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',
 '/usr/local/share/fonts/NotoSansSC-Regular.otf',
 '/usr/local/share/fonts/STKAITI.TTF', # here
 '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf',
 '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf']

但是!下面的方法找不到!

In [8]: [f.name for f in matplotlib.font_manager.fontManager.ttflist]
Out[8]:
['cmb10',
 'cmr10',
 'STIXGeneral',
 'STIXSizeOneSym',
 'DejaVu Sans Mono',
 'STIXSizeTwoSym',
 'cmex10',
 'DejaVu Serif',
 'DejaVu Sans',
 'cmss10',
 'STIXGeneral',
 'cmtt10',
 'DejaVu Sans',
 'cmmi10',
 'STIXSizeThreeSym',
 'STIXSizeTwoSym',
 'STIXSizeFourSym',
 'DejaVu Serif',
 'STIXSizeFiveSym',
 'DejaVu Sans Mono',
 'DejaVu Sans Mono',
 'STIXNonUnicode',
 'STIXGeneral',
 'cmsy10',
 'DejaVu Sans',
 'STIXSizeOneSym',
 'STIXNonUnicode',
 'STIXSizeFourSym',
 'DejaVu Serif Display',
 'STIXSizeThreeSym',
 'DejaVu Sans Mono',
 'STIXNonUnicode',
 'STIXNonUnicode',
 'STIXGeneral',
 'DejaVu Sans',
 'DejaVu Sans Display',
 'DejaVu Serif',
 'DejaVu Serif',
 'DejaVu Sans Mono',
 'DejaVu Sans',
 'DejaVu Serif',
 'DejaVu Serif',
 'DejaVu Sans',
 'DejaVu Sans Mono']

即便如此:

In [10]: matplotlib.font_manager.findfont("STKaiti", rebuild_if_missing=True)
findfont: Font family ['STKaiti'] not found. Falling back to DejaVu Sans.
Out[10]: '/usr/local/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf'

In [14]: matplotlib.font_manager.findfont("STIXSizeTwoSym", rebuild_if_missing=True)
Out[14]: '/usr/local/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf/STIXSizTwoSymReg.ttf'

In [19]: matplotlib.font_manager.findfont("STKaiti", directory="/usr/local/share/fonts/",rebuild_if_missing=True)
findfont: Font family ['STKaiti'] not found. Falling back to DejaVu Sans.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [19], in ()
----> 1 matplotlib.font_manager.findfont("STKaiti", directory="/usr/local/share/fonts/",rebuild_if_missing=True)

File /usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py:1307, in FontManager.findfont(self, prop, fontext, directory, fallback_to_default, rebuild_if_missing)
   1301 # Pass the relevant rcParams (and the font manager, as `self`) to
   1302 # _findfont_cached so to prevent using a stale cache entry after an
   1303 # rcParam was changed.
   1304 rc_params = tuple(tuple(rcParams[key]) for key in [
   1305     "font.serif", "font.sans-serif", "font.cursive", "font.fantasy",
   1306     "font.monospace"])
-> 1307 return self._findfont_cached(
   1308     prop, fontext, directory, fallback_to_default, rebuild_if_missing,
   1309     rc_params)

File /usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py:1361, in FontManager._findfont_cached(self, prop, fontext, directory, fallback_to_default, rebuild_if_missing, rc_params)
   1359     default_prop = prop.copy()
   1360     default_prop.set_family(self.defaultFamily[fontext])
-> 1361     return self.findfont(default_prop, fontext, directory,
   1362                          fallback_to_default=False)
   1363 else:
   1364     raise ValueError(f"Failed to find font {prop}, and fallback "
   1365                      f"to the default font was disabled")

File /usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py:1307, in FontManager.findfont(self, prop, fontext, directory, fallback_to_default, rebuild_if_missing)
   1301 # Pass the relevant rcParams (and the font manager, as `self`) to
   1302 # _findfont_cached so to prevent using a stale cache entry after an
   1303 # rcParam was changed.
   1304 rc_params = tuple(tuple(rcParams[key]) for key in [
   1305     "font.serif", "font.sans-serif", "font.cursive", "font.fantasy",
   1306     "font.monospace"])
-> 1307 return self._findfont_cached(
   1308     prop, fontext, directory, fallback_to_default, rebuild_if_missing,
   1309     rc_params)

File /usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py:1364, in FontManager._findfont_cached(self, prop, fontext, directory, fallback_to_default, rebuild_if_missing, rc_params)
   1361         return self.findfont(default_prop, fontext, directory,
   1362                              fallback_to_default=False)
   1363     else:
-> 1364         raise ValueError(f"Failed to find font {prop}, and fallback "
   1365                          f"to the default font was disabled")
   1366 else:
   1367     _log.debug('findfont: Matching %s to %s (%r) with score of %f.',
   1368                prop, best_font.name, best_font.fname, best_score)

ValueError: Failed to find font DejaVu Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=16.0, and fallback to the default font was disabled

那么,哪种方法真正定义了matplotlib可以使用,如何添加字体?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像