matplotlib Axes.plot()与pyplot.plot() [英] matplotlib Axes.plot() vs pyplot.plot()
问题描述
Axes.plot()
和pyplot.plot()
方法之间有什么区别?一个人是否将另一个人用作子例程?
What is the difference between the Axes.plot()
and pyplot.plot()
methods? Does one use another as a subroutine?
看来我的绘图选项是
line = plt.plot(data)
或
ax = plt.axes()
line = ax.plot(data)
甚至
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?
推荐答案
对于绘制单个图,最佳实践可能是
For drawing a single plot, the best practice is probably
fig = plt.figure()
plt.plot(data)
fig.show()
现在,让我们看一下问题中的3个示例,并说明它们的作用.
Now, lets take a look in to 3 examples from the queston and explain what they do.
-
获取当前图形和轴(如果不存在,将创建一个新的图形和轴)并绘制到其中.
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)
如果要绘制到多个轴(可能在一个图中),则必须使用这种使用ax.plot(...)
的方法.例如,当使用子图.
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.
显式创建新图形-您不会在前一个图形上添加任何内容. 明确创建具有给定矩形形状的新轴,其余的是 与2相同.
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)
使用figure.add_axes
的可能问题是它可能会添加新的轴对象
到该图,它将覆盖第一个(或其他).如果发生这种情况
请求的大小与现有大小不匹配.
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.
这篇关于matplotlib Axes.plot()与pyplot.plot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!