如何在matplotlib中使用XKCD字体

17

我看了这篇文章

我试着复制这里的例子

这是我的效果

enter image description here

这是应该的效果

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

这里是我使用的后端

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

@lincolnfrias,你说的是我在帖子中引用的第一件事吗?我已经看过了 ;) - baxx
@lincolnfrias “在我的情况下…” < 这是指什么?您已经在类似的设置上测试了我发布的代码,还是您指的是另一件事?谢谢。 - baxx
在主目录中,我有一个包含matplotlib文件夹的.config文件夹。我在那里删除了fontList.cache文件。在其他设置中,人们会有一个单独的.matplotlib文件夹(在他们的主目录的第一级,而不是在.config内部)。这在我上面链接的帖子的答案(以及对它的评论)中有详细说明。 - lincolnfrias
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - baxx
2
@MartinThoma 没有,我还没有解决这个问题,也许它会被添加到千禧年大奖问题中。 - baxx
显示剩余7条评论
3个回答

7

使用 matplotlib==3.4.3,我需要执行以下操作:

import shutil
import matplotlib

shutil.rmtree(matplotlib.get_cachedir())

这对我起作用了(Python 3.10,Ubuntu 22.04,matplotlib 3.6.2)。我不得不重新启动笔记本内核(并记得删除删除缓存的那些行,以便它们不会再次运行),才能生效。 - MRule

6
尝试删除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
这对我有用(Python 3.10,Ubuntu 22.04,matplotlib 3.6.2)。我不得不重新启动笔记本内核才能生效。 - MRule

2
如果在安装HumorSans字体后仍然看到错误,我建议运行以下命令:

import matplotlib
matplotlib.font_manager._rebuild()

它将重新构建字体缓存。最初的回答

这在matplotlib 3.6.2中会出现错误。 - MRule
错误信息是什么? - Learning is a mess
我得到了:“AttributeError:模块'matplotlib.font_manager'没有属性'_rebuild'”。 - Rich
可能的解决方案在这里 - Learning is a mess

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接