Python 推荐的绘图方式是:matplotlib 还是 pylab?

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

Which is the recommended way to plot: matplotlib or pylab?

pythonmatplotlib

提问by Ashwin Nanjappa

I see that I can plotin Pythonusing either:

我发现我可以使用以下任一方法在Python 中绘图

import matplotlib
matplotlib.pyplot.plot(...)

Or:

或者:

import pylab
pylab.plot(...)

Both of these use the same matplotlibplotting code.

这两者都使用相同的matplotlib绘图代码。

So, which of these do the Matplotlib developers / docs currently recommend as the better method to plot? Why?

那么,Matplotlib 开发人员/文档目前推荐哪些作为更好的绘图方法?为什么?

采纳答案by tacaswell

Official docs: Matplotlib, pyplot and pylab: how are they related?

官方文档:Matplotlib、pyplot 和 pylab:它们有什么关系?

Both of those imports boil down do doing exactly the same thing and will run the exact same code, it is just different ways of importing the modules.

这两个导入归结起来做完全相同的事情并且将运行完全相同的代码,这只是导入模块的不同方式。

Also note that matplotlibhas two interface layers, a state-machine layer managed by pyplotand the OO interface pyplotis built on top of, see How can I attach a pyplot function to a figure instance?

另请注意,它matplotlib有两个接口层,一个状态机层由其管理,pyplotOO 接口pyplot构建在其之上,请参阅如何将 pyplot 函数附加到图形实例?

pylabis a clean way to bulk import a whole slew of helpful functions (the pyplotstate machine function, most of numpy) into a single name space. The main reason this exists (to my understanding) is to work with ipythonto make a very nice interactive shell which more-or-less replicates MATLAB (to make the transition easier and because it is good for playing around). See pylab.pyand matplotlib/pylab.py

pylab是一种将大量有用的函数(pyplot状态机函数,大部分numpy)批量导入单个名称空间的干净方法。这存在的主要原因(据我所知)是为了ipython制作一个非常好的交互式 shell,它或多或少地复制了 MATLAB(使转换更容易,因为它适合玩)。查看pylab.pymatplotlib/pylab.py

At some level, this is purelya matter of taste and depends a bit on what you are doing.

在某种程度上,这纯粹是一个品味问题,并且在一定程度上取决于您在做什么。

If you are notembedding in a gui (either using a non-interactive backend for bulk scripts or using one of the provided interactive backends) the typical thing to do is

如果您没有嵌入 gui(使用非交互式后端用于批量脚本或使用提供的交互式后端之一),典型的做法是

import matplotlib.pyplot as plt
import numpy as np

plt.plot(....)

which doesn't pollute the name space. I prefer this so I can keep track of where stuff came from.

这不会污染名称空间。我更喜欢这个,所以我可以跟踪东西的来源。

If you use

如果你使用

ipython --pylab

this is equivalent to running

这相当于运行

from pylab import * 

It is now recommended that for new versions of ipythonyou use

现在建议ipython您使用新版本

ipython --matplotlib

which will set up all the proper background details to make the interactive backends to work nicely, but will not bulk import anything. You will need to explicitly import the modules want.

这将设置所有适当的背景细节,以使交互式后端正常工作,但不会批量导入任何内容。您将需要显式导入所需的模块。

import numpy as np
import matplotlib.pyplot as plt

is a good start.

是一个好的开始。

If you are embedding matplotlibin a gui you don't want to import pyplot as that will start extra gui main loops, and exactly what you should import depends on exactly what you are doing.

如果您嵌入matplotlib在 gui 中,则您不想导入 pyplot,因为这会启动额外的 gui 主循环,而您应该导入的确切内容取决于您在做什么。

回答by Apostolos

The documentation at https://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related, which also describes the difference between pyglot and pylab, states: "Although many examples use pylab, it is no longer recommended.". So, I don't see any reason to use pylabor worry about it.

https://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related 上的文档也描述了 pyglot 和 pylab 之间的区别,指出:“尽管许多示例使用pylab,不再推荐。” . 所以,我看不出有任何理由使用pylab或担心它。