本文介绍了插入轴的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想创建一组轴以在父轴组中的特定位置形成一个插入。因此,在 inset_axes 中使用参数 loc = 1,2,3 是不合适的,如下所示: inset_axes = inset_axes(parent_axes, width =30%,#width = 30%parent_bbox height = 1。,#height:1 inch loc = 3) 然而, ,我想要接近这个。和答案这里和这里似乎是比我的问题稍微复杂的问题的答案。 所以问题是,在上面的代码中是否可以替换一个参数,以允许父轴内嵌入轴的自定义位置?我试过使用 bbox_to_anchor ,但不明白它的规范或行为来自文档。具体来说,我试过: inset_axes = inset_axes(parent_axes, width =30%,#width = 30%的parent_bbox height = 1。,#height:1 inch bbox_to_anchor =(0.4,0.1)) 尝试使插入点的左侧和底部的锚点分别为x和y轴的40%和10%。或者,我试图把它放在绝对坐标中: pre $ inset_axes(parent_axes, width =30% ,#width = 30%parent_bbox height = 1。,#height:1 inch bbox_to_anchor =( - 4,-100)) 这些工作都不正确,并给了我一个我无法解释的警告。 更一般地说,好像 loc 是属于 matplotlib ,那么,是否有一个通用的解决方案可以在任何地方使用?看起来这就是 bbox_to_anchor ,但是我再次无法弄清楚如何正确使用它。 解决方案您采取的方法原则上是正确的。然而,就像放置图例与 bbox_to_anchor 一样,位置被确定为 bbox_to_anchor 和 loc 之间的相互作用。上面链接答案中的大部分解释也适用于此处。 /inset_locator_api.html#mpl_toolkits.axes_grid1.inset_locator.inset_axesrel =nofollow noreferrer> inset_axes 是 loc = 1 (右上角)。这意味着如果你指定 bbox_to_anchor =(0.4,0.1),那些将是右上角的坐标,而不是左下角的坐标。 因此,您需要指定 loc = 3 以使插入点的左下角位于(0.4,0.1)。但是,如果没有以相对单位指定宽度和高度(30%),则指定边界为2元组是合理的。 / code>)。换句话说,为了使用相对单位,您需要为 bbox_to_anchor 使用四元组符号。 如果在轴单位中指定 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 ='左上角', 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 ='左上角', bbox_to_anchor =(0.5,0,1,1),bbox_transform = ax.transAxes ) 但是,文档错误或者当前matplotlib版本中存在一些错误 ,因此上述内容不起作用。 (这是一个文档问题,应该很快修复 )。 另一个选项是使用 InsetPosition 而不是 inset_axes 并给现有轴一个新的位置。 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])$ b $ b ip = InsetPosition(ax,[0.4,0.1,0.3,0.7])#posx,posy ,宽度,高度 iax.set_axes_locator(ip) iax.plot([1,2,4]) plt.show() 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 = 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. 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. 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? It seems like that's what bbox_to_anchor is but again, I can't figure out how to use it correctly. 解决方案 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. Most of the explanation in the above linked answer applies here as well.The default loc for inset_axes is loc=1 ("upper right"). 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.You would therefore need to specify loc=3 to have the lower left corner of the inset positionned at (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%"). Or in other words, in order to use relative units you need to use a 4-tuple notation for the bbox_to_anchor.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.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)However, either the documentation is wrong or there is some bug in the current matplotlib version, such that the above will not work. (This was a documentation issue, which should soon be fixed.)Another option is to use InsetPosition instead of inset_axes and to give an existing axes a new position. 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.import matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1.inset_locator import InsetPositionfig, ax= plt.subplots()iax = plt.axes([0, 0, 1, 1])ip = InsetPosition(ax, [0.4, 0.1, 0.3, 0.7]) #posx, posy, width, heightiax.set_axes_locator(ip)iax.plot([1,2,4])plt.show() 这篇关于插入轴的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 21:49