specific location for inset axes

前端 未结 2 1514
太阳男子
太阳男子 2020-12-15 09:51

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<

2条回答
  •  忘掉有多难
    2020-12-15 10:08

    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. 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_y is the y location of the lower left corner of the bounding box in the parent axis coordinates

    bbox_w is the width of the bounding box in parent axis coordinates

    bbox_h is the height of the bounding box in parent axis coordinates

    eps is a small number to get the rectangles to show up from under axes when drawing the rectangular bounding box.

    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.

    The trickiest part for me was realizing that the height and width inputs (when specified as percents) are relative to the bounding box size. 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. 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?

    Another thing was that the loc parameter specifies where to anchor the inset axes within the bounding box. As far as I can tell that's the only function of that parameter.

提交回复
热议问题