ipython --pylab vs ipython

一笑奈何 提交于 2020-01-09 14:30:27

问题


What does ipython --pylab exactly do?

Is ipython --pylab exactly equivalent to:

 > ipython
 > from pylab import *

If not, what are the differences?

Say I launch IPython without the --pylab arguments, how can I bring it to the same state as if I had started it with --pylab?


回答1:


--pylab[=option] is almost technically equivalent to %pylab option as the difference that you cannot un-pylab a --pylab kernel, but you can restart a %pylab kernel.

%pylab is a little more that just from pylab import * (see %pylab?for a longer explanation), but in short yes it imports a lot of things, but it also hooks event loops (qt, wx, osx...) and set-up some display hooks for matplotlib (the things that magically allow you to get inline graph). Setting the display-hook is closer to something like sympy.init_printing() if you wonder.

Note that starting at IPython 1.0 we recommend not to use --pylab or %pylab (unless you know exactly the implication). We provide %matplotlib that only init the display hook. %pylab will warn you if it replaced a few object in current namespace, and which ones. This is useful especially for functions like sum which do not have the same behavior the behavior with and without pylab and leads to subtle bugs.

We consider now that --pylab was a mistake, but that it was still really usefull at the beginning of IPython. We all know that Explicit is better than implicit so if you can advise people not to use %pylab we would appreciate it, to one day get rid of it.

Extract from %pylab help that give only the import part of pylab:

%pylab makes the following imports::

import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot

from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *



回答2:


One noticeable difference besides the imports is the interactive plotting, which you can enable dynamically with:

import matplotlib
matplotlib.rcParams['interactive'] = True



回答3:


I think the --pylab option on the command line is equivalent to using the %pylab magic. At least that is how I have used it. That also gives you the opportunity to choose plotting backend, i.e. %pylab inline, %pylab qt, etc.



来源:https://stackoverflow.com/questions/20525459/ipython-pylab-vs-ipython

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