Ipython Notebook: Default Initialization Python Code

眉间皱痕 提交于 2019-12-22 10:11:12

问题


I use iPython Notebook for scientific applications and I'm always plotting experimental data sets. The default image rendering and color cycling behavior of the combination of iPython Notebook and Matplotlib is pretty bad, but it's great after the following tweaks.

# ipython notebook specific
%pylab inline

# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats

# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)

I don't want to have to enter these manually in every notebook. How can I have these executed for all notebooks I'll ever create?


回答1:


You can create Python files in $HOME/.ipython/profile_default/startup/ that contains the code you want executed at startup.

For example, you can create $HOME/.ipython/profile_default/startup/00-init.py:

# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats

# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)

Note that IPython magics are not supported here, so %matplotlib inline won't work. This question deals with making %matplotlib inline the default.

You should be aware that if you change the defaults for your IPython environment, your notebooks may not work on other people's IPython installations.



来源:https://stackoverflow.com/questions/32438235/ipython-notebook-default-initialization-python-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!