在IPython笔记本中自动运行%matplotlib内联

在IPython笔记本中自动运行%matplotlib内联,python,matplotlib,ipython-notebook,Python,Matplotlib,Ipython Notebook,每次启动IPython笔记本时,我运行的第一个命令是 %matplotlib inline 是否有办法更改我的配置文件,以便在启动IPython时,它会自动处于此模式?我想您可能需要从命令行运行以下命令: ipython notebook --matplotlib=inline 如果您不喜欢每次都在cmd行中键入它,那么您可以创建一个别名来执行此操作。配置方式 IPython具有用于配置的配置文件,位于~/.IPython/profile.*。默认配置文件称为profile\u defaul

每次启动IPython笔记本时,我运行的第一个命令是

%matplotlib inline

是否有办法更改我的配置文件,以便在启动IPython时,它会自动处于此模式?

我想您可能需要从命令行运行以下命令:

ipython notebook --matplotlib=inline
如果您不喜欢每次都在cmd行中键入它,那么您可以创建一个别名来执行此操作。

配置方式 IPython具有用于配置的配置文件,位于
~/.IPython/profile.*
。默认配置文件称为
profile\u default
。此文件夹中有两个主要配置文件:

  • ipython_config.py
  • ipython\u kernel\u config.py
将matplotlib的内联选项添加到
ipython\u kernel\u config.py

c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"
matplotlib vs.pylab 使用
%pylab
获取内联打印是不正确的

它向您的命名空间中引入了各种您不需要的黏液

另一方面,
%matplotlib
在不注入命名空间的情况下启用内联打印。您需要执行显式调用才能导入matplotlib和numpy

import matplotlib.pyplot as plt
import numpy as np

显式输入导入的代价很小,但您现在有了可复制的代码,这一事实应该可以完全克服。

ipython\u config.py
文件中,搜索以下行

# c.InteractiveShellApp.matplotlib = None

并取消对它们的注释。然后,将
None
更改为您正在使用的后端(我使用
'qt4'
)并保存文件。重新启动IPython,应加载matplotlib和pylab-您可以使用
dir()
命令验证哪些模块位于全局命名空间中。

在(当前)IPython 3.2.0(Python 2或3)中

打开隐藏文件夹中的配置文件。ipython

~/.ipython/profile_default/ipython_kernel_config.py
添加以下行

c.IPKernelApp.matplotlib = 'inline'
直接加在后面

c = get_config()

关于@Kyle Kelley和@DGrady,以下是可在

$HOME/.ipython/profile\u default/ipython\u kernel\u config.py
(或您创建的任何配置文件)

改变

# Configure matplotlib for interactive use with the default matplotlib backend.
# c.IPKernelApp.matplotlib = none


这将在ipython qtconsole和笔记本会话中都起作用。

在Jupyter 5.X和更高版本中,通过添加以下代码禁用了该设置

pylab = Unicode('disabled', config=True,
    help=_("""
    DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
    """)
)

@observe('pylab')
def _update_pylab(self, change):
    """when --pylab is specified, display a warning and exit"""
    if change['new'] != 'warn':
        backend = ' %s' % change['new']
    else:
        backend = ''
    self.log.error(_("Support for specifying --pylab on the command line has been removed."))
    self.log.error(
        _("Please use `%pylab{0}` or `%matplotlib{0}` in the notebook itself.").format(backend)
    )
    self.exit(1)
在以前的版本中,这主要是一个警告。但这不是一个大问题,因为Jupyter使用了
内核的概念,您可以通过运行下面的命令来找到项目的内核

$ jupyter kernelspec list
Available kernels:
  python3    /Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin/../share/jupyter/kernels/python3
$ python -m ipykernel_launcher --help
IPython: an enhanced interactive Python shell.

Subcommands
-----------

Subcommands are launched as `ipython-kernel cmd [args]`. For information on
using subcommand 'cmd', do: `ipython-kernel cmd -h`.

install
    Install the IPython kernel

Options
-------

Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.

....
--pylab=<CaselessStrEnum> (InteractiveShellApp.pylab)
    Default: None
    Choices: ['auto', 'agg', 'gtk', 'gtk3', 'inline', 'ipympl', 'nbagg', 'notebook', 'osx', 'pdf', 'ps', 'qt', 'qt4', 'qt5', 'svg', 'tk', 'widget', 'wx']
    Pre-load matplotlib and numpy for interactive use, selecting a particular
    matplotlib backend and loop integration.
--matplotlib=<CaselessStrEnum> (InteractiveShellApp.matplotlib)
    Default: None
    Choices: ['auto', 'agg', 'gtk', 'gtk3', 'inline', 'ipympl', 'nbagg', 'notebook', 'osx', 'pdf', 'ps', 'qt', 'qt4', 'qt5', 'svg', 'tk', 'widget', 'wx']
    Configure matplotlib for interactive use with the default matplotlib
    backend.
...    
To see all available configurables, use `--help-all`
这为我提供了内核文件夹的路径。现在,如果我打开
/Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin//share/jupyter/kernels/python3/kernel.json
文件,我会看到如下内容

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}",
 ],
 "display_name": "Python 3",
 "language": "python"
}
因此,您可以看到执行什么命令来启动内核。因此,如果您运行下面的命令

$ jupyter kernelspec list
Available kernels:
  python3    /Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin/../share/jupyter/kernels/python3
$ python -m ipykernel_launcher --help
IPython: an enhanced interactive Python shell.

Subcommands
-----------

Subcommands are launched as `ipython-kernel cmd [args]`. For information on
using subcommand 'cmd', do: `ipython-kernel cmd -h`.

install
    Install the IPython kernel

Options
-------

Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.

....
--pylab=<CaselessStrEnum> (InteractiveShellApp.pylab)
    Default: None
    Choices: ['auto', 'agg', 'gtk', 'gtk3', 'inline', 'ipympl', 'nbagg', 'notebook', 'osx', 'pdf', 'ps', 'qt', 'qt4', 'qt5', 'svg', 'tk', 'widget', 'wx']
    Pre-load matplotlib and numpy for interactive use, selecting a particular
    matplotlib backend and loop integration.
--matplotlib=<CaselessStrEnum> (InteractiveShellApp.matplotlib)
    Default: None
    Choices: ['auto', 'agg', 'gtk', 'gtk3', 'inline', 'ipympl', 'nbagg', 'notebook', 'osx', 'pdf', 'ps', 'qt', 'qt4', 'qt5', 'svg', 'tk', 'widget', 'wx']
    Configure matplotlib for interactive use with the default matplotlib
    backend.
...    
To see all available configurables, use `--help-all`
如果我运行
jupyter笔记本
,图形将自动
内联

注意,下面的方法仍然有效,您可以在下面的路径上创建一个文件

~/.ipython/profile\u default/ipython\u kernel\u config.py

c = get_config()
c.IPKernelApp.matplotlib = 'inline'
但这种方法的缺点是,这对使用python的每个环境都有全局影响。如果你想在一个变化的环境中有一个共同的行为,你可以认为这是一个优势。


因此,请根据您的需求选择要使用的方法

~/.ipython/profile\u default/startup/
中创建任何
.py
文件,其中包含

get_ipython().magic('matplotlib inline')

“ipython-pylab”有效吗?如果有效,您可以将ipython别名为ipython,这样做非常简单。
ipython--matplotlib
更好,请忽略悬赏。所选答案适用于5.5.0。我会在规定的24小时后结束悬赏。对不起!我打赌你们花了更多的时间来输入这个问题并尝试实现这个解决方案,而不是简单地将它粘贴到笔记本的开头:DThanks。我在matplotlib文档中看到了这个配置选项,但不确定它是否只是设置了matplotlib后端,该后端在您手动调用
%matplotlib
后生效,或者它是否同时设置了默认后端并自动设置为立即在iPython环境中使用。要添加到@Kyle Kelley的编辑中,关于
matplotlib
vs
pylab
,请执行以下操作:,iPython使每次使用概要文件启动时自动执行任意python代码变得非常容易。我相信这是很常见的,有一个配置文件,您可以自动执行常见的导入,如
导入numpy作为np;作为pd进口大熊猫;将matplotlib.pyplot作为plt导入等。注意:
pylab
pyplot
不同。我一定花了一个月的时间才意识到这一点。这(以及SillyBear的回答)停止了与iPython3的合作。建议使用“c.IPKernel.matplotlib”。。。这也不管用。为我工作。在iPython3中,显然有一个新的配置文件,
IPython\u kernel\u config.py
,其中包含此选项。创建新配置文件(
ipython profile create test
)以获取默认配置文件。此选项似乎已重命名为
c.InteractiveShellApp.matplotlib=“inline”
请将帖子更改为
--matplotlib inline
并删除--pylab内容。请参阅ipython devel的这篇文章为什么:关于
matplotlib=inline
:它会减慢您启动的每个内核的速度,而不管您是否要使用matplotlib。这不再有效(至少从ipython 4开始)。忽略命令行选项
--matplotlib
--pylab
。Jupyter命令行帮助的帮助说明这些选项已禁用,应改用
%pylab
%matplotlib
get_ipython().magic('matplotlib inline')