在 IPython Notebook 中自动内联运行 %matplotlib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21176731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically run %matplotlib inline in IPython Notebook
提问by 8one6
Every time I launch IPython Notebook, the first command I run is
每次启动 IPython Notebook 时,我运行的第一个命令是
%matplotlib inline
Is there some way to change my config file so that when I launch IPython, it is automatically in this mode?
有什么方法可以更改我的配置文件,以便当我启动 IPython 时,它会自动处于这种模式?
采纳答案by Kyle Kelley
The configuration way
配置方式
IPython has profiles for configuration, located at ~/.ipython/profile_*. The default profile is called profile_default. Within this folder there are two primary configuration files:
IPython 有配置文件,位于~/.ipython/profile_*. 默认配置文件称为profile_default。在这个文件夹中有两个主要的配置文件:
ipython_config.pyipython_kernel_config.py
ipython_config.pyipython_kernel_config.py
Add the inline option for matplotlib to ipython_kernel_config.py:
将 matplotlib 的内联选项添加到ipython_kernel_config.py:
c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"
matplotlib vs. pylab
matplotlib 与 pylab
Usage of %pylabto get inline plotting is discouraged.
不鼓励使用%pylab来获得内联绘图。
It introduces all sorts of gunk into your namespace that you just don't need.
它在您的命名空间中引入了您不需要的各种垃圾。
%matplotlibon the other hand enables inline plotting without injecting your namespace. You'll need to do explicit calls to get matplotlib and numpy imported.
%matplotlib另一方面,无需注入命名空间即可启用内联绘图。您需要进行显式调用才能导入 matplotlib 和 numpy。
import matplotlib.pyplot as plt
import numpy as np
The small price of typing out your imports explicitly should be completely overcome by the fact that you now have reproducible code.
由于您现在拥有可重现的代码,因此应该完全克服显式输入导入的小成本。
回答by SillyBear
I think what you want might be to run the following from the command line:
我认为您想要的可能是从命令行运行以下命令:
ipython notebook --matplotlib=inline
If you don't like typing it at the cmd line every time then you could create an alias to do it for you.
如果您不喜欢每次都在 cmd 行输入它,那么您可以创建一个别名来为您执行此操作。
回答by MattDMo
In your ipython_config.pyfile, search for the following lines
在您的ipython_config.py文件中,搜索以下几行
# c.InteractiveShellApp.matplotlib = None
and
和
# c.InteractiveShellApp.pylab = None
and uncomment them. Then, change Noneto the backend that you're using (I use 'qt4') and save the file. Restart IPython, and matplotlib and pylab should be loaded - you can use the dir()command to verify which modules are in the global namespace.
并取消注释它们。然后,更改None到您正在使用的后端(我使用'qt4')并保存文件。重启IPython,应该会加载matplotlib和pylab——你可以使用dir()命令来验证哪些模块在全局命名空间中。
回答by memebrain
In (the current) IPython 3.2.0 (Python 2 or 3)
在(当前)IPython 3.2.0(Python 2 或 3)中
Open the configuration file within the hidden folder .ipython
打开隐藏文件夹 .ipython 中的配置文件
~/.ipython/profile_default/ipython_kernel_config.py
add the following line
添加以下行
c.IPKernelApp.matplotlib = 'inline'
add it straight after
之后直接添加
c = get_config()
回答by Chris Hanning
Further to @Kyle Kelley and @DGrady, here is the entry which can be found in the
除了@Kyle Kelley 和@DGrady,这里是可以在
$HOME/.ipython/profile_default/ipython_kernel_config.py(or whichever profile you have created)
$HOME/.ipython/profile_default/ipython_kernel_config.py(或您创建的任何个人资料)
Change
改变
# 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'
This will then work in both ipython qtconsole and notebook sessions.
这将适用于 ipython qtconsole 和 notebook 会话。
回答by Tarun Lalwani
The setting was disabled in Jupyter 5.Xand higher by adding below code
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)
And in previous versions it has majorly been a warning. But this not a big issue because Jupyter uses concepts of kernelsand you can find kernel for your project by running below command
在以前的版本中,它主要是一个警告。但这不是一个大问题,因为 Jupyter 使用 的概念,kernels您可以通过运行以下命令为您的项目找到内核
$ jupyter kernelspec list
Available kernels:
python3 /Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin/../share/jupyter/kernels/python3
This gives me the path to the kernel folder. Now if I open the /Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin/../share/jupyter/kernels/python3/kernel.jsonfile, I see something like below
这给了我内核文件夹的路径。现在,如果我打开/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"
}
So you can see what command is executed to launch the kernel. So if you run the below command
所以你可以看到执行了什么命令来启动内核。所以如果你运行下面的命令
$ 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`
So now if we update our kernel.jsonfile to
所以现在如果我们将kernel.json文件更新为
{
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}",
"--pylab",
"inline"
],
"display_name": "Python 3",
"language": "python"
}
And if I run jupyter notebookthe graphs are automatically inline
如果我运行jupyter notebook图表是自动的inline
Note the below approach also still works, where you create a file on below path
请注意,以下方法仍然有效,您可以在以下路径上创建文件
~/.ipython/profile_default/ipython_kernel_config.py
~/.ipython/profile_default/ipython_kernel_config.py
c = get_config()
c.IPKernelApp.matplotlib = 'inline'
But the disadvantage of this approach is that this is a global impact on every environment using python. You can consider that as an advantage also if you want to have a common behaviour across environments with a single change.
但是这种方法的缺点是这对使用 python 的每个环境都有全局影响。如果您希望通过单个更改跨环境具有共同行为,您也可以将其视为优势。
So choose which approach you would like to use based on your requirement
因此,请根据您的要求选择您想使用的方法
回答by flow2k
Create any .pyfile in ~/.ipython/profile_default/startup/containing
.py在~/.ipython/profile_default/startup/包含中创建任何文件
get_ipython().magic('matplotlib inline')


