Python matplotlib Axes.plot() 与 pyplot.plot()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43482191/
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
matplotlib Axes.plot() vs pyplot.plot()
提问by dkv
What is the difference between the Axes.plot()
and pyplot.plot()
methods? Does one use another as a subroutine?
Axes.plot()
和pyplot.plot()
方法和有什么不一样?是否使用另一个作为子程序?
It seems that my options for plotting are
似乎我的绘图选项是
line = plt.plot(data)
or
或者
ax = plt.axes()
line = ax.plot(data)
or even
甚至
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
line = ax.plot(data)
Are there situations where it is preferable to use one over the other?
是否存在使用一种优于另一种的情况?
采纳答案by matusko
For drawing a single plot, the best practice is probably
对于绘制单个图,最佳实践可能是
fig = plt.figure()
plt.plot(data)
fig.show()
Now, lets take a look in to 3 examples from the queston and explain what they do.
现在,让我们看一下 queston 中的 3 个示例并解释它们的作用。
Takes the current figure and axes (if none exists it will create a new one) and plot into them.
line = plt.plot(data)
In your case, the behavior is same as before with explicitly stating the axes for plot.
ax = plt.axes() line = ax.plot(data)
This approach of using
ax.plot(...)
is a must, if you want to plot into multiple axes (possibly in one figure). For example when using a subplots.Explicitly creates new figure - you will not add anything to previous one. Explicitly creates a new axes with given rectangle shape and the rest is the same as with 2.
fig = plt.figure() ax = fig.add_axes([0,0,1,1]) line = ax.plot(data)
possible problem using
figure.add_axes
is that it may add a new axes object to the figure, which will overlay the first one (or others). This happens if the requested size does not match the existing ones.
获取当前图形和轴(如果不存在,它将创建一个新的)并绘制到它们中。
line = plt.plot(data)
在您的情况下,行为与之前相同,明确说明绘图轴。
ax = plt.axes() line = ax.plot(data)
ax.plot(...)
如果您想绘制多个轴(可能在一个图中),则必须使用这种方法。例如,当使用subplots 时。显式创建新图形 - 您不会向前一个图形添加任何内容。显式创建一个具有给定矩形形状的新轴,其余与 2 相同。
fig = plt.figure() ax = fig.add_axes([0,0,1,1]) line = ax.plot(data)
使用的可能问题
figure.add_axes
是它可能会向图形添加一个新的轴对象,该对象将覆盖第一个(或其他)。如果请求的大小与现有大小不匹配,则会发生这种情况。
回答by ImportanceOfBeingErnest
There is essentially no difference. plt.plot
will at some point (after making sure that there is a figure and an axes available to plot to) call the plot function from that axes instance.
本质上没有区别。plt.plot
将在某个时候(在确保有一个图形和一个可用于绘制的轴之后)从该轴实例调用 plot 函数。
So the main difference is rather at the user's side:
所以主要区别在于用户方面:
- do you want to use the Matlab-like state machine approach, which may save some lines of code for simple plotting tasks? Then use
pyplot
. - do you want to have full control over the plotting using the more pythonic object oriented approach? Then use objects like axes explicitely.
- 你想使用类似于 Matlab 的状态机方法,它可以为简单的绘图任务节省一些代码行吗?然后使用
pyplot
. - 您想使用更 Pythonic 的面向对象方法完全控制绘图吗?然后明确地使用像轴这样的对象。
You may want to read the matplotlib usage guide.
您可能需要阅读 matplotlib使用指南。
回答by kyramichel
Pyplot's plotting methods can be applied to either the Pyplot root (pyplot.plot()) or an axes object (axes.plot()).
Pyplot 的绘图方法可以应用于 Pyplot 根 (pyplot.plot()) 或轴对象 (axes.plot())。
Calling a plotting function directly on the Pyplot library (pyplot.plot()) creates a default subplot (figure and axes). Calling it on an axes object (axes.plot()) requires that you to have created your own axes object already and puts the graph onto that customized plotting space.
直接在 Pyplot 库 (pyplot.plot()) 上调用绘图函数会创建一个默认的子图(图形和轴)。在轴对象 (axes.plot()) 上调用它要求您已经创建了自己的轴对象并将图形放到该自定义绘图空间中。
While pyplot.plot() is easy to use, you have more control over your space (and better able to understand interaction with other libraries) if you create an axes object axes.plot().
虽然 pyplot.plot() 易于使用,但如果您创建轴对象axes.plot(),您可以更好地控制您的空间(并且能够更好地理解与其他库的交互)。
Axes.plot() returns an axes object. Every axes object has a parent figure object. The axes object contains the methods for plotting, as well as most customization options, while the figure object stores all of the figure-level attributes and allow the plot to output as an image.
Axes.plot() 返回一个轴对象。每个轴对象都有一个父图形对象。轴对象包含绘图方法以及大多数自定义选项,而图形对象存储所有图形级别的属性并允许绘图输出为图像。
If you use pyplot.plot() method and want to start customizing your axes, you can find out the name of the default axes object it created by calling pyplot.gca() to "get current axes."
如果您使用 pyplot.plot() 方法并想开始自定义您的轴,您可以通过调用 pyplot.gca() 来“获取当前轴”来找出它创建的默认轴对象的名称。
回答by Salam
python plt.plot(): it will create many default subplot and save many lines of code and easy to understand
python plt.plot():会创建很多默认的子图,保存多行代码,易于理解
Axes.plot(): using axes object will give you more ability to customize your plot space
Axes.plot():使用轴对象将使您更有能力自定义绘图空间