使用matplotlib获取xkcd图

时间:2013-10-29 16:29:43

标签: matplotlib

当前版本的matplotlib(1.3.1)支持xkcd风格的绘图,但是当我follow the basic instructions生成这样的绘图时(在iPython 1.1.0中),

%pylab
plt.xkcd()
plt.plot(sin(linspace(0, 10)))
plt.title('Whoo Hoo!!!')

我得到了

enter image description here

而不是

enter image description here

我做错了什么?

4 个答案:

答案 0 :(得分:14)

要使其正常运作,您需要

  • matplotlib 1.3.1(它不能使用matplotlib 1.3.0或更早版本)
    • sudo pip install matplotlib --upgrade
  • font Humor Sans
    • herehere下载,安装(在OS X上打开它并单击安装)
  • 删除matplotlib字体缓存(由DanHickstein in matplotlib/issues/2269建议)
    • rm ~/.matplotlib/fontList.cache

现在,执行代码,它应该工作。

不需要将后端更改为TkAgg,正如有些人所说的那样。对我而言,它在'module://IPython.kernel.zmq.pylab.backend_inline'上完全正常(您可以通过plt.get_backend()进行检查。)

(我有同样的问题,我已经解决了它,至少在OS X 10.8.5上,使用matplotlib 1.3.1和IPython 2.0.0;删除字体缓存是必要的,以使字体运行。)

答案 1 :(得分:3)

使用ubuntu 16.04和python 3,安装了matplotlib 2.0.0,以下内容为我解决了这个问题。

  1. 安装漫画:sudo apt install fonts-humor-sans
  2. 删除matplotlib缓存:rm ~/.cache/matplotlib -r

答案 2 :(得分:1)

确保您已安装 fonts-humor-sans

在 Ubuntu/Debian 中,您可以使用(从命令行)安装它们:

<块引用>

$ sudo apt install python3-xkcd fonts-humor-sans

那么最好的选择(而不是删除 matplotlib 缓存)是重建字体管理器(从 Python 解释器、脚本或 Jupyter 笔记本中):

<块引用>

导入 matplotlib
matplotlib.font_manager._rebuild()

答案 3 :(得分:0)

来自blog post you linked

  

此外,如果您希望上面的字体匹配,请务必在您的系统上下载并安装Humor Sans字体。要使matplotlib识别它,您可能必须删除字体缓存...

至于要下载哪个版本的字体,我会尝试最新的。