%matplotlib inline 在 iPython 和 Jupyter 控制台上不起作用

%matplotlib inline doesn't work on iPython and Jupyter console

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

该图显示在第 6 行 运行 之后的单独 window 中,而第 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 控制台时,它说 inline 是一个 Unknown 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 本身。然而,Vim 在 IPython 控制台中调用时工作得很好。

我正在使用 matplotlib 2.0.0

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

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

我不确定为什么它没有抛出错误,但在我的情况下却抛出错误:

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

或在浏览器的 jupyter notebook 中

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

%matplotlib 没有 inline 适合我(我使用 osx 和 IPython 7.0.1)