每次我启动IPython Notebook时,我运行的第一个命令是
%matplotlib inline
有没有办法更改我的配置文件,使得当我启动IPython时,它自动进入这种模式?
每次我启动IPython Notebook时,我运行的第一个命令是
%matplotlib inline
有没有办法更改我的配置文件,使得当我启动IPython时,它自动进入这种模式?
IPython有用于配置的配置文件,位于~/.ipython/profile_*
下。默认配置文件称为profile_default
。在此文件夹中,有两个主要的配置文件:
ipython_config.py
ipython_kernel_config.py
将inline选项添加到ipython_kernel_config.py
中的matplotlib:
c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"
%pylab
的使用被不建议。它会将许多您不需要的东西注入到您的命名空间中。
另一方面,%matplotlib
可以在不注入您的命名空间的情况下启用内联绘图。您需要显式调用以导入matplotlib和numpy。
import matplotlib.pyplot as plt
import numpy as np
通过明确地输入您的导入模块,所需的工作量微不足道,而您现在具有可重现的代码这一事实完全可以弥补此点成本。
%matplotlib
时生效,还是它既设置了默认后端,又自动设置以立即在iPython环境中使用。 - 8one6matplotlib
与pylab
,在@Kyle Kelley的编辑意见之外,我想补充一些内容。iPython使用Profile非常容易实现每次启动自动执行任意Python代码功能。通常会有一个Profile专门用来自动导入常用的包,例如import numpy as np; import pandas as pd; import matplotlib.pyplot as plt
等等。请注意,pylab
并不等同于pyplot
,这个误解曾经困扰了我整整一个月。 - 8one6ipython_kernel_config.py
,其中包含这个选项。创建一个新的配置文件(ipython profile create test
)以获取默认设置。 - DGradyc.InteractiveShellApp.matplotlib =“inline”
。 - tiago通过添加以下代码,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
这给了我内核文件夹的路径。现在如果我打开/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'
ipython notebook --matplotlib=inline
如果您不喜欢每次在cmd命令行中输入它,那么您可以创建一个别名来代替您进行操作。
--matplotlib inline
并删除 --pylab
部分。请参阅此 ipython 开发人员的帖子,了解原因:http://carreau.github.io/posts/10-No-PyLab-Thanks.ipynb.html - Jakobmatplotlib=inline
的一点说明:它会减慢您启动的每个内核,无论您是否想使用 matplotlib。 - Kyle Kelley%pylab
或 %matplotlib
代替。 - Cas在 ~/.ipython/profile_default/startup/
目录下创建任何一个 .py
文件,包含以下内容:
get_ipython().magic('matplotlib inline')
get_ipython().run_line_magic('matplotlib', 'inline')
。 - Alper在(当前的)IPython 3.2.0(Python 2或3)中,
打开隐藏文件夹.ipython中的配置文件。
~/.ipython/profile_default/ipython_kernel_config.py
请添加下面这行代码
c.IPKernelApp.matplotlib = 'inline'
在此之后直接添加
c = get_config()
ipython_config.py
文件中,查找以下行。# c.InteractiveShellApp.matplotlib = None
并且
# c.InteractiveShellApp.pylab = None
取消注释下列代码。然后将 None
更改为您正在使用的后端(我使用 'qt4'
),保存文件。重新启动 IPython,matplotlib 和 pylab 将被加载-您可以使用 dir()
命令验证哪些模块在全局命名空间中。
# Configure matplotlib for interactive use with the default matplotlib backend.
# c.IPKernelApp.matplotlib = none
to
# Configure matplotlib for interactive use with the default matplotlib backend.
c.IPKernelApp.matplotlib = 'inline'
这样做可以在IPython QT控制台和笔记本会话中都起作用。