[英]Python Matplotlib xkcd: Fonts Humor Sans missing
I have tried to install xkcd fonts 'Humor Sans', but unfortunately, I am not able to use the fonts. Here, is what I did so far:我尝试安装 xkcd fonts 'Humor Sans',但不幸的是,我无法使用 fonts。这是我到目前为止所做的:
%pylab inline
plt.xkcd()
plt.plot(sin(linspace(0,10)))
plt.title('Whoo Hoo!!!')
The plot, with the exception of the fonts is working as expected. plot(fonts 除外)按预期工作。 The error message gives me, what I expected:
错误消息给了我我所期望的:
findfont: Font family ['xkcd', 'xkcd Script', 'Humor Sans', 'Comic Sans MS'] not found. findfont:未找到字体系列 ['xkcd'、'xkcd Script'、'Humor Sans'、'Comic Sans MS']。 Falling back to DejaVu Sans.
回到 DejaVu Sans。
The font manager does not find any Humor* Sans although in the Settings -> General -> Fonts, I do find the fonts.尽管在设置 -> 常规 -> Fonts 中,字体管理器没有找到任何 Humor* Sans,但我确实找到了 fonts。
import matplotlib.font_manager
x = [f.name for f in matplotlib.font_manager.fontManager.ttflist if f.name.startswith('Humor')]
print(set(x))
the outcome is an empty result:结果是一个空结果:
set()放()
Since, the folder structure is not clear using the iPad, i have copied the HumorSans.ttf file in a folder "Fonts", eg由于使用 iPad 时文件夹结构不清晰,我已将 HumorSans.ttf 文件复制到“字体”文件夹中,例如
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)
This works as expected.这按预期工作。 The fonts is the Humor Sans fonts, where I have explicitly changed the fonts. For all other texts, eg yticklabels, the fonts is the standard fonts such that the problem is still there.
fonts 是 Humor Sans fonts,我已经明确更改了 fonts。对于所有其他文本,例如 yticklabels,fonts 是标准的 fonts,因此问题仍然存在。
How can I correctly install, either directly or load in each notebook the fonts. The problem is that i don't have access to the folders, I need to work in, eg matplotlib.get_cachedir() /private/var/mobile/Containers/Data/Application/AAD3....5693/tmp/matplotlib-nu6taz9_我如何直接或在每个笔记本中正确安装 fonts。问题是我无权访问文件夹,我需要在其中工作,例如 matplotlib.get_cachedir() /private/var/mobile/Containers /数据/应用程序/AAD3....5693/tmp/matplotlib-nu6taz9_
Any help?有什么帮助吗?
Solved!解决了!
Luke Davis Helped with his post: How can I configure matplotlib to be able to read fonts from a local path? Luke Davis 在他的帖子中提供了帮助: 如何配置 matplotlib 以便能够从本地路径读取 fonts?
#!/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}.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.