简体   繁体   English

插入轴的具体位置

[英]specific location for inset axes

I want to create a set of axes to form an inset at a specific location in the parent set of axes. 我想创建一组轴,以在父组轴的特定位置形成插入。 It is therefore not appropriate to just use the parameter loc=1,2,3 in the inset_axes as shown here: 因此,在inset_axes使用参数loc=1,2,3inset_axes ,如下所示:

inset_axes = inset_axes(parent_axes,
                    width="30%", # width = 30% of parent_bbox
                    height=1., # height : 1 inch
                    loc=3)

However, I would like something close to this. 但是,我想要接近这一点。 And the answers here and here seem to be answers to questions slightly more complicated than mine. 这里这里的答案似乎是比我更复杂的问题的答案。

So, the question is is there a parameter that I can replace in the above code that will allow custom locations of the inset axes within the parent axes? 所以,问题是我可以在上面的代码中替换一个参数,它允许插入轴在父轴中的自定义位置吗? I've tried to use the bbox_to_anchor but do not understand it's specification or behavior from the documentation . 我曾尝试使用bbox_to_anchor但不理解文档中的规范或行为。 Specifically I've tried: 具体来说,我尝试过:

 inset_axes = inset_axes(parent_axes,
                        width="30%", # width = 30% of parent_bbox
                        height=1., # height : 1 inch
                        bbox_to_anchor=(0.4,0.1))

to try to get the anchor for the left and bottom of the inset to be at 40% and 10% of the x and y axis respectively. 尝试使插图的左侧和底部的锚点分别位于x轴和y轴的40%和10%处。 Or, I tried to put it in absolute coordinates: 或者,我试图把它放在绝对坐标中:

inset_axes = inset_axes(parent_axes,
                            width="30%", # width = 30% of parent_bbox
                            height=1., # height : 1 inch
                            bbox_to_anchor=(-4,-100))

Neither of these worked correctly and gave me a warning that I couldn't interpret. 这些都没有正常工作,并给了我一个我无法解释的警告。

More generally, it seems like loc is a pretty standard parameter in many functions belonging to matplotlib , so, is there a general solution to this problem that can be used anywhere? 更一般地说,似乎loc在属于matplotlib许多函数中都是一个非常标准的参数,那么,这个问题有一个通用的解决方案可以在任何地方使用吗? It seems like that's what bbox_to_anchor is but again, I can't figure out how to use it correctly. 看起来这就是bbox_to_anchor ,但是再一次,我无法弄清楚如何正确使用它。

The approach you took is in principle correct. 你采取的方法原则上是正确的。 However, just like when placing a legend with bbox_to_anchor , the location is determined as an interplay between bbox_to_anchor and loc . 但是,就像使用bbox_to_anchor 放置图例 bbox_to_anchor ,该位置被确定为bbox_to_anchorloc之间的相互作用。 Most of the explanation in the above linked answer applies here as well. 上述链接答案中的大多数解释也适用于此。

The default loc for inset_axes is loc=1 ("upper right"). inset_axes的默认locloc=1 (“右上角”)。 This means that if you you specify bbox_to_anchor=(0.4,0.1) , those will be the coordinates of the upper right corner, not the lower left one. 这意味着如果您指定bbox_to_anchor=(0.4,0.1) ,那么它们将是右上角的坐标,而不是左下角的坐标。
You would therefore need to specify loc=3 to have the lower left corner of the inset positionned at (0.4,0.1) . 因此,您需要指定loc=3以使插入的左下角定位在(0.4,0.1)

However, specifying a bounding as a 2-tuple only makes sense if not specifying the width and height in relative units ( "30%" ). 但是,如果不以相对单位( "30%" )指定宽度和高度,则将边界指定为2元组才有意义。 Or in other words, in order to use relative units you need to use a 4-tuple notation for the bbox_to_anchor . 或者换句话说,为了使用相对单位,您需要为bbox_to_anchor使用4元组表示法。

In case of specifying the bbox_to_anchor in axes units one needs to use the bbox_transform argument, again, just as with legends explained here , and set it to ax.transAxes . 如果在轴单位中指定bbox_to_anchor ,则需要再次使用bbox_transform参数,就像这里解释的图例一样,并将其设置为ax.transAxes

plt.figure(figsize=(6,3))
ax = plt.subplot(221)
ax.set_title("100%, (0.5,1-0.3,.3,.3)")
ax.plot(xdata, ydata)
axins = inset_axes(ax, width="100%", height="100%", loc='upper left',
                   bbox_to_anchor=(0.5,1-0.3,.3,.3), bbox_transform=ax.transAxes)


ax = plt.subplot(222)
ax.set_title("30%, (0.5,0,1,1)")
ax.plot(xdata, ydata)
axins = inset_axes(ax, width="30%", height="30%", loc='upper left',
                   bbox_to_anchor=(0.5,0,1,1), bbox_transform=ax.transAxes)

在此输入图像描述

Find a complete example on the matplotlib page: Inset Locator Demo 在matplotlib页面上找到完整的示例: Inset Locator Demo

Another option is to use InsetPosition instead of inset_axes and to give an existing axes a new position. 另一个选择是使用InsetPosition而不是inset_axes并为现有轴提供一个新位置。 InsetPosition takes the x and y coordinates of the lower left corner of the axes in normalized axes coordinates, as well as the width and height as input. InsetPosition将标准化轴坐标中轴的左下角的x和y坐标以及宽度和高度作为输入。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition

fig, ax= plt.subplots()

iax = plt.axes([0, 0, 1, 1])
ip = InsetPosition(ax, [0.4, 0.1, 0.3, 0.7]) #posx, posy, width, height
iax.set_axes_locator(ip)

iax.plot([1,2,4])
plt.show()

Finally one should mention that from matplotlib 3.0 on, you can use matplotlib.axes.Axes.inset_axes 最后应该提一下,从matplotlib 3.0开始,你可以使用matplotlib.axes.Axes.inset_axes

import matplotlib.pyplot as plt

plt.figure(figsize=(6,3))
ax = plt.subplot(221)
ax.set_title("ax.inset_axes, (0.5,1-0.3,.3,.3)")
ax.plot([0,4], [0,10])
axins = ax.inset_axes((0.5,1-0.3,.3,.3))

plt.show()

The result is roughly the same, except that mpl_toolkits.axes_grid1.inset_locator.inset_axes allows for a padding around the axes (and applies it by default), while Axes.inset_axes does not have this kind of padding. 结果大致相同,只是mpl_toolkits.axes_grid1.inset_locator.inset_axes允许在轴周围填充(并默认应用它),而Axes.inset_axes没有这种填充。

在此输入图像描述

Using the answer from ImportanceOfBeingErnest and several of the suggested links from the unreleased matplotlib documentation like the locator demo and the inset_axes docs , it still took me some time to figure out how all the parameters behaved. 使用来自ImportanceOfBeingErnest的答案以及未发布的matplotlib文档中的几个建议链接(如定位器演示inset_axes文档) ,我仍然花了一些时间来弄清楚所有参数的表现。 So, I will repeat my understanding here for clarity. 所以,为了清楚起见,我将在此重复我的理解。 I ended up using: 我最终使用:

bbox_ll_x = 0.2
bbox_ll_y = 0
bbox_w = 1
bbox_h = 1
eps = 0.01
inset_axes = inset_axes(parent_axes, 
               height="30%", #height of inset axes as frac of bounding box
               width="70%",  #width of inset axes as frac of bounding box
               bbox_to_anchor=(bbox_ll_x,bbox_ll_y,bbox_w-bbox_ll_x,bbox_h), 
               loc='upper left',
               bbox_transform=parent_axes.transAxes)

parent_axes.add_patch(plt.Rectangle((bbox_ll_x, bbox_ll_y+eps),
               bbox_w-eps-bbox_ll_x, 
               bbox_h-eps, 
               ls="--", 
               ec="c", 
               fc="None",
               transform=parent_axes.transAxes))

bbox_ll_x is the x location of the lower left corner of the bounding box in the parent axis coordinates (that is the meaning of the bbox_transform input) bbox_ll_x是父轴坐标中边界框左下角的x位置(这是bbox_transform输入的含义)

bbox_ll_y is the y location of the lower left corner of the bounding box in the parent axis coordinates bbox_ll_y是父轴坐标中边界框左下角的y位置

bbox_w is the width of the bounding box in parent axis coordinates bbox_w是父轴坐标中边界框的宽度

bbox_h is the height of the bounding box in parent axis coordinates bbox_h是父轴坐标中边界框的高度

eps is a small number to get the rectangles to show up from under axes when drawing the rectangular bounding box. eps是一个小数字,用于在绘制矩形边界框时使矩形从轴下方显示。

I used the add_patch call in order to put a cyan dashed line that represents the inner edge of the bounding box that is drawn. 我使用add_patch调用来放置一条青色虚线,表示绘制的边界框的内边缘。

The trickiest part for me was realizing that the height and width inputs (when specified as percents) are relative to the bounding box size. 对我来说最棘手的部分是意识到heightwidth输入(当指定为百分比时)相对于边界框大小。 That's why (as noted in the links and the answer below) you must specify a 4-tuple for the bbox_to_anchor parameter if you specify the size of the inset axes in percents. 这就是为什么(如下面的链接和答案中所述),如果以百分比形式指定插入轴的大小,则必须bbox_to_anchor参数指定4元组。 If you specify the size of the inset axes as percents and don't supply bbox_w or bbox_h how can matplotlib get the absolute size of the inset? 如果您将插入轴的大小指定为百分比并且提供bbox_wbbox_hmatplotlib如何获得插入的绝对大小?

Another thing was that the loc parameter specifies where to anchor the inset axes within the bounding box. 另一件事是loc参数指定在边界框内锚定插入轴的位置。 As far as I can tell that's the only function of that parameter. 据我所知,这是该参数的唯一功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM