[英]Automatically run %matplotlib inline in IPython Notebook
每次启动IPython Notebook时,我运行的第一个命令是
%matplotlib inline
有没有办法改变我的配置文件,以便在我启动IPython时,它会自动进入这种模式?
IPython有配置文件,位于~/.ipython/profile_*
。 默认配置文件称为profile_default
。 在此文件夹中有两个主要配置文件:
ipython_config.py
ipython_kernel_config.py
将matplotlib的内联选项添加到ipython_kernel_config.py
:
c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"
不鼓励使用%pylab
进行内联绘图。
它将各种gunk引入您不需要的命名空间。
另一方面, %matplotlib
在不注入命名空间的情况下实现内联绘图。 你需要进行显式调用才能获得matplotlib和numpy导入。
import matplotlib.pyplot as plt
import numpy as np
显而易见地输入您的输入的小代价应该通过您现在具有可重现的代码完全克服。
我想你想要的是从命令行运行以下命令:
ipython notebook --matplotlib=inline
如果您不喜欢每次都在cmd行输入它,那么您可以创建一个别名来为您完成。
通过添加以下代码,在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使用kernels
概念,你可以通过运行以下命令找到你的项目的内核
$ jupyter kernelspec list
Available kernels:
python3 /Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin/../share/jupyter/kernels/python3
这给了我内核文件夹的路径。 现在,如果我打开/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"
}
因此,您可以看到执行启动内核的命令。 因此,如果您运行以下命令
$ 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`
所以现在我们将kernel.json
文件更新为
{
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}",
"--pylab",
"inline"
],
"display_name": "Python 3",
"language": "python"
}
如果我运行jupyter notebook
,图表会自动inline
请注意,以下方法仍然有效,您可以在下面的路径上创建文件
〜/ .ipython / profile_default / ipython_kernel_config.py
c = get_config()
c.IPKernelApp.matplotlib = 'inline'
但这种方法的缺点是,这是使用python对每个环境的全局影响。 如果您希望在具有单个更改的环境中具有共同行为,则可以将其视为优势。
因此,根据您的要求选择您想要使用的方法
在ipython_config.py
文件中,搜索以下行
# c.InteractiveShellApp.matplotlib = None
和
# c.InteractiveShellApp.pylab = 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_default/ipython_kernel_config.py
(或您创建的任何配置文件)
更改
# Configure matplotlib for interactive use with the default matplotlib backend.
# c.IPKernelApp.matplotlib = none
至
# Configure matplotlib for interactive use with the default matplotlib backend.
c.IPKernelApp.matplotlib = 'inline'
这将适用于ipython qtconsole和笔记本会话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.