【问题标题】:Python Matplotlib xkcd: Fonts Humor Sans missingPython Matplotlib xkcd:字体 Humor Sans 丢失
【发布时间】:2020-11-11 21:53:05
【问题描述】:

我尝试安装 xkcd 字体“Humor Sans”,但不幸的是,我无法使用这些字体。这是我到目前为止所做的:

  • iPad Pro
  • 已安装 carnets 和 juno(用于 Jupyter 笔记本)
  • Matplotlib 3.1.1 版
  • 安装 iFonts 以安装 Humor Sans
  • 运行 xkcd 示例,例如
%pylab inline
plt.xkcd()
plt.plot(sin(linspace(0,10)))
plt.title('Whoo Hoo!!!')

除字体外,情节按预期工作。错误消息给了我我的预期:

findfont:找不到字体系列 ['xkcd', 'xkcd Script', 'Humor Sans', 'Comic Sans MS']。回到 DejaVu Sans。

字体管理器没有找到任何 Humor* Sans,尽管在设置 -> 常规 -> 字体中,我确实找到了字体。

import matplotlib.font_manager
x = [f.name for f in matplotlib.font_manager.fontManager.ttflist if f.name.startswith('Humor')]
print(set(x))

结果为空结果:

set()

由于使用 iPad 时文件夹结构不清晰,我已将 HumorSans.ttf 文件复制到文件夹“Fonts”中,例如

import matplotlib.font_manager as fm
import matplotlib.pyplot as plot
import numpy as np

font_path = 'Fonts/Humor-Sans.ttf'
my_font = fm.FontProperties(fname=font_path)

plt.xkcd()
fig, ax = plt.subplot()
x = np.linspace(0,3)
y = np.sin(x)

ax.bar(x, y, color='green')
ax.set_xlabel(u'Some x label', fontproperties=my_font)
ax.set_ylabel(u'Some y label', fontproperties=my_font)
ax.set_title(u'Some fancy title', fontproperties=my_font)

for label in ax.get_xticklabels():
    label.set_fontproperties(my_font)

这按预期工作。字体是 Humor Sans 字体,我已经明确更改了字体。对于所有其他文本,例如yticklabels,字体是标准字体,所以问题仍然存在。

我怎样才能正确安装,直接或在每个笔记本中加载字体。问题是我无权访问文件夹,我需要在其中工作,例如matplotlib.get_cachedir() /private/var/mobile/Containers/Data/Application/AAD3....5693/tmp/matplotlib-nu6taz9_

有什么帮助吗?

【问题讨论】:

    标签: python matplotlib fonts


    【解决方案1】:

    解决了!

    Luke Davis 帮助他的帖子:How can I configure matplotlib to be able to read fonts from a local path?

    #!/usr/bin/env python3
    # Imports
    import os
    import re
    import shutil
    from glob import glob
    from matplotlib import matplotlib_fname
    from matplotlib import get_cachedir
    
    # Copy files over
    _dir_data = re.sub('/matplotlibrc$', '', matplotlib_fname())
    dir_source = '<your-font-directory-here>'
    dir_dest = f'{_dir_data}/fonts/ttf'
    # print(f'Transfering .ttf and .otf files from {dir_source} to {dir_dest}.')
    for file in glob(f'{dir_source}/*.[ot]tf'):
        if not os.path.exists(f'{dir_dest}/{os.path.basename(file)}'):
            print(f'Adding font "{os.path.basename(file)}".')
            shutil.copy(file, dir_dest)
    
    # Delete cache
    dir_cache = get_cachedir()
    for file in glob(f'{dir_cache}/*.cache') + glob(f'{dir_cache}/font*'):
        if not os.path.isdir(file): # don't dump the tex.cache folder... because dunno why
            os.remove(file)
            print(f'Deleted font cache {file}.')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-29
      • 2014-10-30
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 2016-08-08
      • 2013-11-08
      相关资源
      最近更新 更多