Python 什么是%pylab?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20961287/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:39:29  来源:igfitidea点击:

What is %pylab?

pythonmatplotlib

提问by Anton

I keep seeing people use %pylabin various code snippits, particularly with iPython. However, I cannot see where %pylabis mentioned anywhere in Learning Python (and the few other Python books I have) and am not really sure what it means.

我一直看到人们使用%pylab各种代码片段,尤其是 iPython。但是,我看不到《%pylab学习 Python》(以及我拥有的其他几本 Python 书籍)中的任何地方提到的内容,也不确定这意味着什么。

I'm sure the answer is simple, but can anyone enlighten me?

我相信答案很简单,但有人能启发我吗?

采纳答案by doug

%pylabis a magic functionin ipython.

%pylab是一个神奇的功能IPython中

Magic functions in ipython always begin with the percent sign (%) followed without any spaces by a small text string; in essence, ipython magic functions define shortcuts particularly useful for interactive work, e.g., to give you an idea of how magic functions work in python, a few of my favorites:

ipython 中的魔术函数总是以百分号 (%) 开头,后面没有任何空格,后面是一个小文本字符串;本质上,ipython 魔术函数定义了对交互式工作特别有用的快捷方式,例如,让您了解魔术函数在 python 中的工作原理,我最喜欢的一些:

  • to view cwd directory contents:

    %ls   
    
  • to run a script in ipython using an empty namespace, type space then a script name:

    %run     
    
  • to execute a code snippet (particularly for multi-line snippets which would usually cause an _IndentationError_ to be thrown):

    %paste
    
  • 查看 cwd 目录内容:

    %ls   
    
  • 要使用空命名空间在 ipython 中运行脚本,请输入空格然后输入脚本名称:

    %run     
    
  • 执行代码片段(特别是对于通常会导致抛出 _IndentationError_ 的多行片段):

    %paste
    

When the %pylabmagic function is entered at the IPython prompt, it triggers the import of various modules within Matplotlib.

%pylab在 IPython 提示符下输入魔法函数时,它会触发 Matplotlib 中各种模块的导入。

Which modules? well, the ones subsumed under the pylabinterface.

哪些模块?好吧,那些包含在pylab界面下的。

The awesome Matplotlib plotting library has twodistinct interfaces: a pythonic one, and the original MATLAB-like one intended for plotting at the interactive prompt.

令人敬畏的 Matplotlib 绘图库有两个不同的接口:一个是Pythonic接口,另一个是原始的类似 MATLAB 的接口,用于在交互式提示下绘图。

The former is usually imported like so:

前者通常是这样导入的:

from matplotlib import pyplot as PLT

Indeed, pyplot has its own magic python magic function

确实,pyplot 有自己的魔术 python 魔术功能

%pyplot

Why two different interfaces? Matplotlib's original interface was pylab; only later was the pythonic interface added. Scripting and app development were not the primary uses cases for Matplotlib when the project began, plotting in the python shell was.

为什么有两个不同的接口?Matplotlib 的原始接口是 pylab;后来才添加了pythonic接口。项目开始时,脚本和应用程序开发不是 Matplotlib 的主要用例,在 python shell 中绘图是。

ApparentlyJohn Hunter (Matplotlib's creator) wanted to include interactive plotting in python so he submitted a patch to Fernando Perez's (FP) IPython project. FP was a Ph.D student at the time and informed JH that he would not able to review the path for some time. As a result, JH created Matplotlib. The significance is that Matplotlib began as a shell-based plotting scheme.

显然,John Hunter(Matplotlib 的创建者)希望在 Python 中包含交互式绘图,因此他向 Fernando Perez(FP)的 IPython 项目提交了一个补丁。FP 当时是一名博士生,并告知 JH,他将有一段时间无法查看路径。结果,JH 创建了 Matplotlib。重要的是,Matplotlib 开始是一个基于 shell 的绘图方案。

the pylab interface is indeed more suitable for interactive work:

pylab界面确实更适合交互工作:

from pylab import *

x, y = arange(10), cos(x/2)
plot(x, y)
show()

and using the pyplot interface:

并使用 pyplot 界面:

from matplotlib import pyplot as PLT
import numpy as NP

x, y = NP.arange(10), NP.cos(x/2)
fig = PLT.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y)
PLT.show()

回答by kindall

As its name implies, Pylab is a MATLAB-like front end for doing mathematics in Python. iPython has specific support for Pylab, which is invoked using the %pylabmagic command.

顾名思义,Pylab 是一个类似于 MATLAB 的前端,用于在 Python 中进行数学运算。iPython 对 Pylab 有特定的支持,它是使用%pylab魔法命令调用的。

回答by richizy

%pylabis a "magic function" that you can call within IPython, or Interactive Python. By invoking it, the IPython interpreter will import matplotliband NumPymodules such that you'll have convenient access to their functions. As an example,

%pylab是一个“魔法函数”,您可以在 IPython 或Interactive Python 中调用它。通过调用它,IPython 解释器将导入matplotlibNumPy模块,以便您可以方便地访问它们的功能。举个例子,

rich@rich-ubuntu:~/working/fb_recruit/working$ ipython
Python 2.7.6 |Anaconda 1.8.0 (64-bit)| (default, Nov 11 2013, 10:47:18) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: arange(4)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-2e43d7eb1b3e> in <module>()
----> 1 arange(4)

NameError: name 'arange' is not defined

In [2]: %pylab
Using matplotlib backend: Qt4Agg
Populating the interactive namespace from numpy and matplotlib

In [3]: arange(4)
Out[3]: array([0, 1, 2, 3])

In [4]: 

回答by Shital Shah

%pylabis shortcut for typing all of below commands which in essence adds numpy and matplotlib in to your session. This was added in IPython as a transition tool and current recommendation is that you should not use it. The core reason is that below sets of commands imports too much in the global namespace and also it doesn't allow you to change the mode for matplotlib from UI to QT or something else. You can get history and reasoning behind this at http://nbviewer.ipython.org/github/Carreau/posts/blob/master/10-No-PyLab-Thanks.ipynb?create=1.

%pylab是键入以下所有命令的快捷方式,这些命令实质上将 numpy 和 matplotlib 添加到您的会话中。这是作为过渡工具添加到 IPython 中的,当前建议您不要使用它。核心原因是以下命令集在全局命名空间中导入了太多内容,并且它不允许您将 matplotlib 的模式从 UI 更改为 QT 或其他内容。你可以在http://nbviewer.ipython.org/github/Carreau/posts/blob/master/10-No-PyLab-Thanks.ipynb?create=1获得历史和推理。

This is what %pylabdoes:

这就是%pylab它的作用:

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

from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *

This is what I use instead at the start of my notebook:

这是我在笔记本开始时使用的:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

回答by hzpc-joostk

More recent documentation about the IPython magics here.

更多关于 IPython 魔法的最新文档在这里

Magics function are often present in the form of shell-like syntax, but are under the hood python function. The syntax and assignment possibility are similar to the one with the bang (!) syntax, but with more flexibility and power. Magic function start with a percent sign (%) or double percent (%%).

Magics 函数通常以类似 shell 的语法形式出现,但在 python 函数的底层。语法和赋值可能性与 bang (!) 语法类似,但具有更大的灵活性和功能。魔术函数以百分号 (%) 或双百分号 (%%) 开头。

A little bit hereand more specifically about the %pylabmagic here.

这里有一点,更具体地说是这里%pylab魔法。

 %pylab [--no-import-all] [gui]

Load numpy and matplotlib to work interactively.

This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session.

加载 numpy 和 matplotlib 以交互工作。

这个函数可以让你在 IPython 会话期间的任何时候激活 pylab(matplotlib、numpy 和交互式支持)。

%pylabmakes the following imports:

%pylab进行以下导入:

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 *