每次我启动 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"
matplotlib 与 pylab
使用 %pylab
来获得内联绘图是 discouraged .
它在你的命名空间中引入了你不需要的各种垃圾。
%matplotlib
另一方面,可以在不注入(inject)命名空间的情况下进行内联绘图。您需要进行显式调用才能导入 matplotlib 和 numpy。
import matplotlib.pyplot as plt
import numpy as np
您现在拥有可重现的代码这一事实完全可以克服显式输入导入的小成本。
关于python - 在 IPython Notebook 中自动运行 %matplotlib inline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21176731/