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

2024-05-09 14:57:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我只是对派拉布有点了解,然后我就糊涂了。 每当我想导入numpy时,我总是可以:

import numpy as np

我只是想,一旦我这样做了

from pylab import *

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

我想问的事情很少:

  1. pylab仅仅是numpy、scipy和matplotlib的包装器,对吗?
  2. 由于np是pylab中的numpy别名,pylab中的scipy和matplotlib别名是什么?(据我所知,plt是matplotlib.pyplot的别名,但我不知道matplotlib本身的别名)

Tags: fromimportnumpymatplotlibasnppltscipy
3条回答
  1. 不,pylabmatplotlib(in matplotlib.pylab)的一部分,并试图给您一个类似于MatLab的环境。matplotlib有许多依赖项,其中numpy是在公共别名np下导入的。scipy不是matplotlib的依赖项。

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

因为一些人(比如我)可能仍然对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.

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

Here is the link以供进一步阅读和其他有用的示例。

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

Matplotlib是python打印库的名称。

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

Pylab与pyplot是相同的,但是有额外的特性(目前不鼓励使用它)。

  • pylab=pyplot+numpy

请在此处查看更多信息:Matplotlib, Pylab, Pyplot, etc: What's the difference between these and when to use each?

相关问题 更多 >