Python add_axes 和 add_subplot 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43326680/
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
What are the differences between add_axes and add_subplot?
提问by Ivan
In a previous answerit was recommended to me to use add_subplot
instead of add_axes
to show axes correctly, but searching the documentation I couldn't understand when and why I should use either one of these functions.
在以前的答案中,建议我使用add_subplot
而不是add_axes
正确显示轴,但搜索文档我无法理解何时以及为什么应该使用这些功能之一。
Can anyone explain the differences?
任何人都可以解释这些差异吗?
回答by ImportanceOfBeingErnest
Common grounds
共同点
Both, add_axes
and add_subplot
add an axes to a figure. They both return a (subclass of a) matplotlib.axes.Axes
object.
两者,add_axes
并add_subplot
为图形添加轴。它们都返回一个(a 的子类)matplotlib.axes.Axes
对象。
However, the mechanism which is used to add the axes differs substantially.
但是,用于添加轴的机制有很大不同。
add_axes
add_axes
The calling signature of add_axes
is add_axes(rect)
, where rect
is a list [x0, y0, width, height]
denoting the lower left point of the new axes in figure coodinates (x0,y0)
and its width and height. So the axes is positionned in absolute coordinates on the canvas. E.g.
的调用签名add_axes
是add_axes(rect)
,其中rect
是一个列表,[x0, y0, width, height]
表示图形坐标中新轴的左下点(x0,y0)
及其宽度和高度。所以轴被定位在画布上的绝对坐标中。例如
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
places a figure in the canvas that is exactly as large as the canvas itself.
在画布中放置一个与画布本身一样大的图形。
add_subplot
add_subplot
The calling signature of add_subplot
does not directly provide the option to place the axes at a predefined position. It rather allows to specify where the axes should be situated according to a subplot grid. The usual and easiest way to specify this position is the 3 integer notation,
的调用签名 add_subplot
不直接提供将轴放置在预定义位置的选项。它允许根据子图网格指定轴应位于的位置。指定这个位置的通常和最简单的方法是 3 整数表示法,
fig = plt.figure()
ax = fig.add_subplot(231)
In this example a new axes is created at the first position (1
) on a grid of 2 rows and 3 columns. To produce only a single axes, add_subplot(111)
would be used (First plot on a 1 by 1 subplot grid). (In newer matplotlib versions, add_subplot()` without any arguments is possible as well.)
在此示例中,在1
2 行 3 列网格的第一个位置 ( )处创建了一个新轴。要仅生成单个轴,add_subplot(111)
将使用(在 1 x 1 子图网格上的第一个图)。(在较新的 matplotlib 版本中,add_subplot() 也可以不带任何参数。)
The advantage of this method is that matplotlib takes care of the exact positioning. By default add_subplot(111)
would produce an axes positioned at [0.125,0.11,0.775,0.77]
or similar, which already leaves enough space around the axes for the title and the (tick)labels. However, this position may also change depending on other elements in the plot, titles set, etc.
It can also be adjusted using pyplot.subplots_adjust(...)
or pyplot.tight_layout()
.
这种方法的优点是 matplotlib 负责精确定位。默认情况下add_subplot(111)
会产生一个定位于[0.125,0.11,0.775,0.77]
或类似的轴,它已经在轴周围为标题和(刻度)标签留下了足够的空间。然而,这个位置也可能会根据情节中的其他元素、标题集等而改变。它也可以使用pyplot.subplots_adjust(...)
或进行调整pyplot.tight_layout()
。
In most cases, add_subplot
would be the prefered method to create axes for plots on a canvas. Only in cases where exact positioning matters, add_axes
might be useful.
在大多数情况下,add_subplot
这将是在画布上为绘图创建轴的首选方法。只有在精确定位很重要的情况下,才add_axes
可能有用。
Example
例子
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (5,3)
fig = plt.figure()
fig.add_subplot(241)
fig.add_subplot(242)
ax = fig.add_subplot(223)
ax.set_title("subplots")
fig.add_axes([0.77,.3,.2,.6])
ax2 =fig.add_axes([0.67,.5,.2,.3])
fig.add_axes([0.6,.1,.35,.3])
ax2.set_title("random axes")
plt.tight_layout()
plt.show()
Alternative
选择
The easiest way to obtain one or more subplots together with their handles is plt.subplots()
. For one axes, use
获得一个或多个子图及其句柄的最简单方法是plt.subplots()
. 对于一个轴,使用
fig, ax = plt.subplots()
or, if more subplots are needed,
或者,如果需要更多子图,
fig, axes = plt.subplots(nrows=3, ncols=4)
The initial question
最初的问题
In the initial questionan axes was placed using fig.add_axes([0,0,1,1])
, such that it sits tight to the figure boundaries. The disadvantage of this is of course that ticks, ticklabels, axes labels and titles are cut off. Therefore I suggested in one of the comments to the answer to use fig.add_subplot
as this will automatically allow for enough space for those elements, and, if this is not enough, can be adjusted using pyplot.subplots_adjust(...)
or pyplot.tight_layout()
.
在最初的问题中,使用 放置轴fig.add_axes([0,0,1,1])
,使其紧靠图形边界。这样做的缺点当然是刻度、刻度标签、轴标签和标题被切断。因此,我在对答案的评论之一中建议使用,fig.add_subplot
因为这将自动为这些元素留出足够的空间,如果这还不够,可以使用pyplot.subplots_adjust(...)
或进行调整pyplot.tight_layout()
。