【问题标题】:Getting xkcd plots using matplotlib使用 matplotlib 获取 xkcd 图
【发布时间】:2013-11-08 22:45:36
【问题描述】:

当前版本的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!!!')

我明白了

而不是

我做错了什么?

【问题讨论】:

  • 相关? “另外,如果你想要上面的字体匹配,请务必在你的系统上下载并安装 Humor Sans 字体。为了让 matplotlib 识别它,你可能必须删除系统上的字体缓存”
  • @jabey:Doh!我怎么错过了?是否有当前推荐的版本(链接已失效)。另外,仅凭缺少的字体无法解释线条,似乎没有被xkcdified。
  • matplotlib 使用什么后端?您可以通过plt.get_backend() 查询。如果您安装了 Qt4,请尝试将其设置为 *Agg 后端,例如 Qt4Agg。如果您在 ipython 笔记本中,您还可以将“内联”后端与 pylab 一起使用。 e:看到这个问题:github.com/matplotlib/matplotlib/issues/2269
  • plt.xkcd 命令采用许多参数来调整它执行的操作量。有关更多信息,请参阅它的文档字符串。
  • @aganders3:MacOSX。我无法切换到 qt,因为这会导致我在尝试安装 sip 时遇到各种错误。

标签: matplotlib


【解决方案1】:

确保您已安装 fonts-humor-sans

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

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

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

导入 matplotlib
matplotlib.font_manager._rebuild()

【讨论】:

    【解决方案2】:

    使用 ubuntu 16.04 和 python 3,并安装了 matplotlib 2.0.0,以下解决了我的问题。

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

    【讨论】:

      【解决方案3】:

      要让它工作,你需要

      • matplotlib 1.3.1(不适用于 matplotlib 1.3.0 或更早版本)
        • sudo pip install matplotlib --upgrade
      • 字体 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;删除字体缓存是使字体运行所必需的。)

      【讨论】:

      • 对于 Ubuntu 14.04 上的 matplotlib 1.3.1,缓存位于 ~/.cache/matplotlib(不确定何时更改),删除它确实至关重要。
      • Ubuntu 12.04 上的 matplotlib 1.4.2 也是如此
      • 1.4.2版本中也可能是/tmp/xdgcache/matplotlib目录
      • Win7 上的 matplotlib 字体缓存:%userprofile%\.matplotlib\fontList.cache - source
      • 我还不得不删除fontList.py3k.cache
      【解决方案4】:

      来自blog post you linked

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

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

      【讨论】:

      • 查看上面的编辑评论:我现在有了字体,但线条仍然没有 xkcdified。
      • @raxacoricofallapatorius 你在使用正确版本的matplotlib吗?
      • 我认为是这样:在问题中添加了版本号。
      • @raxacoricofallapatorius 那时不知道,以前从未在 python 中写过任何东西,只是认为它可能是缺少的字体:P
      • @raxacoricofallapatorius:安装字体后必须清除matplptlib字体缓存:stackoverflow.com/questions/26085867/matplotlib-font-not-found/…
      猜你喜欢
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 2017-09-08
      相关资源
      最近更新 更多