如何在matplotlib中使用XKCD字体

时间:2016-09-21 10:46:03

标签: python python-3.x matplotlib fonts anaconda

I've followed through this post

我正在尝试重现示例from here

This is how mine looks

enter image description here

This is how it should look

enter image description here

代码位于该页面和本帖末尾

我正在使用的系统

$ 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后端

Here is the back end that I'm using

  plt.get_backend()
  Out[42]: 'TkAgg'

输出错误

我已按照帖子here;

的说明进行操作

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()

2 个答案:

答案 0 :(得分:2)

尝试删除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:~$


答案 1 :(得分:1)

如果在安装HumorSans字体后仍然看到错误,建议运行:

import matplotlib
matplotlib.font_manager._rebuild()

它将重建字体缓存。