Python numpy、scipy、matplotlib 和 pylab 之间的混淆

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

Confusion between numpy, scipy, matplotlib and pylab

pythonnumpymatplotlibscipy

提问by goFrendiAsgard

Numpy, scipy, matplotlib, and pylab are common terms among they who use python for scientific computation.

Numpy、scipy、matplotlib 和 pylab 是使用 Python 进行科学计算的常用术语。

I just learn a bit about pylab, and I got confused. Whenever I want to import numpy, I can always do:

我只是对 pylab 了解了一点,我感到困惑。每当我想导入 numpy 时,我总是可以这样做:

import numpy as np

I just consider, that once I do

我只是想,一旦我这样做

from pylab import *

the numpy will be imported as well (with npalias). So basically the second one does more things compared to the first one.

numpy 也将被导入(使用np别名)。所以基本上第二个比第一个做了更多的事情。

There are few things I want to ask:

有几件事我想问:

  1. Is it right that pylab is just a wrapper for numpy, scipy and matplotlib?
  2. As np is the numpy alias in pylab, what is the scipy and matplotlib alias in pylab? (as far as I know, plt is alias of matplotlib.pyplot, but I don't know the alias for the matplotlib itself)
  1. pylab 是否只是 numpy、scipy 和 matplotlib 的包装器?
  2. 由于 np 是 pylab 中的 numpy 别名,那么 pylab 中的 scipy 和 matplotlib 别名是什么?(据我所知,plt 是 matplotlib.pyplot 的别名,但我不知道 matplotlib 本身的别名)

采纳答案by Benjamin Bannier

  1. No, pylabis part of matplotlib(in matplotlib.pylab) and tries to give you a MatLab like environment. matplotlibhas a number of dependencies, among them numpywhich it imports under the common alias np. scipyis not a dependency of matplotlib.

  2. If you run ipython --pylaban automatic import will put all symbols from matplotlib.pylabinto global scope. Like you wrote numpygets imported under the npalias. Symbols from matplotlibare available under the mplalias.

  1. 不,pylabmatplotlib(in matplotlib.pylab) 的一部分,并试图为您提供类似 MatLab 的环境。matplotlib有许多依赖项,numpy其中它在公共别名下导入npscipy不是 的依赖项matplotlib

  2. 如果您运行ipython --pylab自动导入,则会将所有符号matplotlib.pylab放入全局范围内。就像你写的一样numpy,在np别名下导入。来自别名的符号matplotlib可用mpl

回答by Felipe Almeida

Scipy and numpyare scientific projects whose aim is to bring efficient and fast numeric computing to python.

Scipy 和 numpy是科学项目,旨在为 Python 带来高效、快速的数值计算。

Matplotlibis the name of the python plotting library.

Matplotlib是 python 绘图库的名称。

Pyplotis an interactive api for matplotlib, mostly for use in notebooks like jupyter. You generally use it like this: import matplotlib.pyplot as plt.

Pyplot是 matplotlib 的交互式 api,主要用于像 jupyter 这样的笔记本。您通常像这样使用它:import matplotlib.pyplot as plt.

Pylabis the same thing as pyplot, but with extra features (its use is currently discouraged).

Pylab与 pyplot 相同,但具有额外的功能(目前不鼓励使用它)。

  • pylab= pyplot + numpy
  • pylab= pyplot + numpy

See more information here: Matplotlib, Pylab, Pyplot, etc: What's the difference between these and when to use each?

在此处查看更多信息:Matplotlib、Pylab、Pyplot 等:它们之间的区别是什么以及何时使用它们?

回答by jamescampbell

Since some people (like me) may still be confused about usage of pylab since examples using pylabare out there on the internet, here is a quote from the official matplotlib FAQ:

由于一些人(像我一样)可能仍然对 pylab 的使用感到困惑,因为使用的示例pylab在互联网上有,这里引用来自官方 matplotlib 常见问题解答:

pylab is a convenience module that bulk imports matplotlib.pyplot (for plotting) and numpy (for mathematics and working with arrays) in a single name space. Although many examples use pylab, it is no longer recommended.

pylab 是一个方便的模块,它在单个名称空间中批量导入 matplotlib.pyplot(用于绘图)和 numpy(用于数学和处理数组)。虽然很多例子使用了pylab,但不再推荐使用。

So, TL;DR; is do not use pylab, period. Use pyplotand import numpyseparately as needed.

所以,TL; DR; 是不要使用pylab,句号。根据需要单独使用pyplot和导入numpy

Here is the linkfor further reading and other useful examples.

这是进一步阅读和其他有用示例的链接