Python matplotlib.pyplot与matplotlib.pylab

Python matplotlib.pyplot与matplotlib.pylab,python,matplotlib,Python,Matplotlib,我通常使用以下软件包创建绘图:matplotlib.pylab。但是,还有一个名为matplotlib.pyplot的包 在使用它们时,我无法发现两者之间的任何差异。因此,我的问题如下: 包matplotlib.pylab和matplotlib.pyplot之间有什么区别。在哪种情况下,您会建议一种而不是另一种?根据: Pyplot为底层打印提供状态机接口 matplotlib中的库。这意味着图形和轴是隐式的 并自动创建以实现所需的绘图 Pylab将pyplot功能(用于打印)与numpy相结合

我通常使用以下软件包创建绘图:
matplotlib.pylab
。但是,还有一个名为
matplotlib.pyplot
的包

在使用它们时,我无法发现两者之间的任何差异。因此,我的问题如下:

matplotlib.pylab
matplotlib.pyplot
之间有什么区别。在哪种情况下,您会建议一种而不是另一种?

根据:

Pyplot为底层打印提供状态机接口 matplotlib中的库。这意味着图形和轴是隐式的 并自动创建以实现所需的绘图

Pylab将pyplot功能(用于打印)与numpy相结合 功能(用于数学和处理数组) 单一名称空间,使该名称空间(或环境)更加 我喜欢。例如,可以只调用sin和cos函数 就像在MATLAB中一样,同时拥有 pyplot

pyplot界面通常是非交互式的首选界面 绘图(即脚本)。pylab界面便于 交互式计算和绘图,因为它最大限度地减少了键入。(我的重点。)

注意

from pylab import *
也执行

from numpy import *
这将覆盖许多内置Python函数,例如:

In [5]: import __builtin__
In [6]: import numpy as np

In [5]: {name for name in set(dir(np)).intersection(dir(__builtin__)) if not name.startswith('__') and getattr(__builtin__, name) != getattr(np, name)}
Out[5]: {'abs', 'all', 'any', 'max', 'min', 'round', 'sum'}
因此,我不喜欢pylab import*中的
(或者对于任何模块来说都不喜欢module import*
中的
),因为它会使众所周知的受欢迎的Python名称以意外的方式运行(如果您不总是记住numpy import*
中的
已经污染了全局名称空间)

比如说,

In [32]: np.all([np.arange(3), np.arange(3)])
Out[32]: False

复制品?也可能是重复的
In [33]: all([np.arange(3), np.arange(3)])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()