add_axes和add_subplot有什么区别? [英] What are the differences between add_axes and add_subplot?

查看:1011
本文介绍了add_axes和add_subplot有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前的 answer 中,建议我使用add_subplot而不是add_axes来正确显示轴,但是在搜索文档时,我无法理解何时以及为什么应该使用这些功能之一.

任何人都可以解释这些差异吗?

解决方案

共同点

两者都 add_axes add_subplot 添加一个轴到一个数字.它们都返回matplotlib.axes.Axes对象(的子类).

但是,用于添加轴的机制大不相同.

add_axes

add_axes add_axes(rect),其中rect是列表[x0, y0, width, height],表示图中新轴的左下点与(x0,y0)及其宽度和高度相关.因此,将轴定位在画布上的绝对坐标中.例如.

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])

在画布上放置一个与画布本身大小完全一样的图形.

add_subplot

add_subplot 不直接提供将轴放置在预定义位置的选项.而是允许根据子图网格指定轴的位置.指定此位置的通常且最简单的方法是3整数符号

fig = plt.figure()
ax = fig.add_subplot(231)

在此示例中,在2行3列的网格上的第一个位置(1)创建了一个新轴.为了仅产生单个轴,将使用add_subplot(111)(在1 x 1子图网格上的第一个图). (在较新的matplotlib版本中,也可以使用不带任何参数的 add_subplot()`.)

此方法的优点是matplotlib负责精确定位.默认情况下,add_subplot(111)会产生一个位于[0.125,0.11,0.775,0.77]或类似位置的轴,该轴周围已经为标题和(tick)标签留了足够的空间.但是,此位置也可能根据情节中的其他元素,标题集等而改变. 也可以使用pyplot.subplots_adjust(...)pyplot.tight_layout()进行调整.

在大多数情况下,add_subplot是在画布上为绘图创建轴的首选方法.仅在精确定位很重要的情况下,add_axes才有用.

示例

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()

替代

获得一个或多个子图及其句柄的最简单方法是 .对于一个轴,使用

fig, ax = plt.subplots()

或者,如果需要更多子图,

fig, axes = plt.subplots(nrows=3, ncols=4)

最初的问题

初始问题中使用fig.add_axes([0,0,1,1])放置轴,使其紧靠图形边界.这样的缺点当然是剔除了刻度,刻度标签,轴标签和标题.因此,我在答案的注释之一中建议使用fig.add_subplot,因为这将自动为这些元素留出足够的空间,如果不够用的话,可以使用pyplot.subplots_adjust(...)pyplot.tight_layout()进行调整.

In a previous answer it 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.

Can anyone explain the differences?

解决方案

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.

However, the mechanism which is used to add the axes differs substantially.

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.

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

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,

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.)

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().

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.

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

fig, ax = plt.subplots()

or, if more subplots are needed,

fig, axes = plt.subplots(nrows=3, ncols=4)

The initial question

In the initial question an 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().

这篇关于add_axes和add_subplot有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆