我正在尝试复制示例从这里开始
代码在那页上,在这篇文章的末尾。
我正在使用的系统
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
我正在使用的Python版本
3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
Matplotlib版本
import matplotlib
matplotlib.__version__
Out[37]: '1.5.1'
安装在系统上的Humorsans字体
这表明我已经安装了字体。
$ fc-list | grep "Humo"
/usr/share/fonts/Humor-Sans-1.0.ttf: Humor Sans:style=Regular
/home/vco/.fonts/Humor-Sans-1.0.ttf: Humor Sans:style=Regular
我正在使用的Matplotlib后端
plt.get_backend()
Out[42]: 'TkAgg'
输出误差
我遵循了一个帖子这里的指示;
来自python输出的错误:
/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1288:
UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not
found. Falling back to Bitstream Vera Sans
(prop.get_family(), self.defaultFamily[fontext]))
/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1298:
UserWarning: findfont: Could not match :family=Bitstream Vera
Sans:style=normal:variant=normal:weight=400:stretch=normal:size=medium.
Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmtt10.ttf UserWarning)
我还尝试将下载的Humorsans字体移到
/usr/share/matplotlib/mpl-data/fonts/ttf
但仍然有以下错误
/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans
(prop.get_family(), self.defaultFamily[fontext]))
/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1298: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=400:stretch=normal:size=medium. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmtt10.ttf
UserWarning)
/home/vco/anaconda/envs/math_general/lib/python3.5/site-packages/matplotlib/font_manager.py:1298: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=400:stretch=normal:size=large. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmtt10.ttf
UserWarning)
代码
from matplotlib import pyplot as plt
import numpy as np
plt.xkcd()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.xticks([])
plt.yticks([])
ax.set_ylim([-30, 10])
data = np.ones(100)
data[70:] -= np.arange(30)
plt.annotate(
'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
plt.plot(data)
plt.xlabel('time')
plt.ylabel('my overall health')
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.bar([-0.125, 1.0-0.125], [0, 100], 0.25)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.set_xticks([0, 1])
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([0, 110])
ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
plt.yticks([])
plt.title("CLAIMS OF SUPERNATURAL POWERS")
plt.show()
发布于 2019-03-11 02:27:39
尝试删除Matplotlib缓存并查看它是否工作(即rm -r ~/.cache/matplotlib
)。
就我而言,它解决了这个问题:
user@host:~$ ./xkcd.py
/home/user/.pyenv/versions/3.7.1/lib/python3.7/site-packages/matplotlib/font_manager.py:1241:
UserWarning: findfont: Font family ['xkcd', 'Humor Sans', 'Comic Sans MS'] not found.
Falling back to DejaVu Sans. (prop.get_family(), self.defaultFamily[fontext]))
user@host:~$ rm -r ~/.cache/matplotlib
user@host:~$ ./xkcd.py
user@host:~$
发布于 2019-06-12 15:54:53
如果您在安装HumorSans字体后仍然看到错误,我建议运行:
import matplotlib
matplotlib.font_manager._rebuild()
它将重新生成字体缓存。
发布于 2022-06-10 10:45:06
对于matplotlib==3.4.3
,我不得不这样做:
import shutil
import matplotlib
shutil.rmtree(matplotlib.get_cachedir())
https://stackoverflow.com/questions/39614381
复制相似问题