【问题标题】:%matplotlib inline doesn't work on iPython and Jupyter console%matplotlib inline 在 iPython 和 Jupyter 控制台上不起作用
【发布时间】:2017-03-06 02:49:44
【问题描述】:

我是第一次尝试 Jupyter 控制台,但无法使用 %matplotlib inline 魔法。下面是一个示例会话的屏幕截图:

运行第 6 行后,绘图显示在一个单独的窗口中,而第 7 行没有执行任何操作。

当我运行%matplotlib --list 时,inline 作为选项之一给出:

Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt',
'nbagg', 'agg', 'gtk', 'tk', 'ipympl', 'inline']

当我尝试使用另一个后端时,比如qt5,它会给出一条错误消息,因为我没有安装任何 Qt。

ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide
package to be installed, but it was not found.

运行%matplotlib?? 读取:

 If you are using the inline matplotlib backend in the IPython Notebook
        you can set which figure formats are enabled using the following::

            In [1]: from IPython.display import set_matplotlib_formats

            In [2]: set_matplotlib_formats('pdf', 'svg')

        The default for inline figures sets `bbox_inches` to 'tight'. This can
        cause discrepancies between the displayed image and the identical
        image created using `savefig`. This behavior can be disabled using the
        `%config` magic::

            In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}

但我不知道是否可以通过调整来解决我的问题。

当我尝试使用神奇的 IPython 控制台时,它显示 inlineUnknown Backend

UnknownBackend: No event loop integration for u'inline'. Supported event loops are: qt, 
qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx

在谷歌搜索后我也找到了this issue on github,但我什至不知道这是否与我的情况有关(他们的大部分谈话对我来说没有意义哈哈)。

最后,我不确定这个问题是否与此相关,但为了以防万一:当我尝试通过 !vim 命令在 Jupyter 中打开 Vim 时,它出现了非常严重的故障,使我无法甚至在不完全关闭终端的情况下退出 Jupyter 本身。然而,在 IPython 控制台中调用时,Vim 工作得非常好。

我正在使用matplotlib 2.0.0

如果有人能帮我解决这个问题,那就太好了!谢谢!

【问题讨论】:

    标签: python matplotlib ipython jupyter


    【解决方案1】:

    您正在运行一个完全基于文本且无法显示图像的控制台。因此,尽管inline 可用,但它不会产生内联输出。

    我不确定为什么它不会引发错误,但在我的情况下它会:

    您可以在 GUI 控制台中使用 %matplotlib inline,例如 Jupyter QTConsole

    或在浏览器中的 jupyter notebook 中

    【讨论】:

    • 我第一次尝试时实际上遇到了同样的错误,但不知何故能够绕过它。不过,我不记得我到底做了什么。无论如何,非常感谢你,这很明显,但我不知道为什么我没有意识到这一点......哈哈。
    【解决方案2】:

    %matplotlib 没有 inline 对我有用(我使用的是 osx 和 IPython 7.0.1)

    【讨论】:

    • 更新:我使用的是 IPython 7.6.1,但 %matplotlib 似乎不再适合我了。
    • 就像布拉德说的那样,你不能将内联图像放入控制台......但我相信 %matplotlib inline 确实对大多数系统有影响 - 没有它,你需要 plt.show() 来在外部窗口中显示图表,它会阻止输入到控制台,直到您关闭它。使用 %matplotlib inline 您不需要 plt.show() 并且它不会阻塞控制台。还值得注意的是,您在 MacOS 上使用 %matplotlib osx 而不是内联。
    【解决方案3】:

    如果您运行的是旧版本的 ipython,请尝试使用 %pylab inline。见notes in this tutorial

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多