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()
现在,让我们看一下 queston 中的 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(...)
的方法是必须的,如果你想绘制到多个轴(可能在一个图中).例如,当使用 subplots 时.
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屋!