插入定位器演示

inset_locatorinset_axes 允许通过指定宽度和高度以及可选地将位置(Loc)接受位置作为代码(类似于图例),轻松地在轴的角中放置插入。默认情况下,通过“边界板”(BorderPad)参数控制的内嵌与轴之间的某些点偏移。

  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.axes_grid1.inset_locator import inset_axes
  3. fig, (ax, ax2) = plt.subplots(1, 2, figsize=[5.5, 2.8])
  4. # Create inset of width 1.3 inches and height 0.9 inches
  5. # at the default upper right location
  6. axins = inset_axes(ax, width=1.3, height=0.9)
  7. # Create inset of width 30% and height 40% of the parent axes' bounding box
  8. # at the lower left corner (loc=3)
  9. axins2 = inset_axes(ax, width="30%", height="40%", loc=3)
  10. # Create inset of mixed specifications in the second subplot;
  11. # width is 30% of parent axes' bounding box and
  12. # height is 1 inch at the upper left corner (loc=2)
  13. axins3 = inset_axes(ax2, width="30%", height=1., loc=2)
  14. # Create an inset in the lower right corner (loc=4) with borderpad=1, i.e.
  15. # 10 points padding (as 10pt is the default fontsize) to the parent axes
  16. axins4 = inset_axes(ax2, width="20%", height="20%", loc=4, borderpad=1)
  17. # Turn ticklabels of insets off
  18. for axi in [axins, axins2, axins3, axins4]:
  19. axi.tick_params(labelleft=False, labelbottom=False)
  20. plt.show()

插入定位器演示

参数bbox_to_anchorbbox_transfrom 可用于对插入位置和大小进行更细粒度的控制,甚至可以将插入位置置于完全任意位置。bbox_to_anchor 根据 bbox_transform 设置坐标中的边界框。

  1. fig = plt.figure(figsize=[5.5, 2.8])
  2. ax = fig.add_subplot(121)
  3. # We use the axes transform as bbox_transform. Therefore the bounding box
  4. # needs to be specified in axes coordinates ((0,0) is the lower left corner
  5. # of the axes, (1,1) is the upper right corner).
  6. # The bounding box (.2, .4, .6, .5) starts at (.2,.4) and ranges to (.8,.9)
  7. # in those coordinates.
  8. # Inside of this bounding box an inset of half the bounding box' width and
  9. # three quarters of the bounding box' height is created. The lower left corner
  10. # of the inset is aligned to the lower left corner of the bounding box (loc=3).
  11. # The inset is then offset by the default 0.5 in units of the font size.
  12. axins = inset_axes(ax, width="50%", height="75%",
  13. bbox_to_anchor=(.2, .4, .6, .5),
  14. bbox_transform=ax.transAxes, loc=3)
  15. # For visualization purposes we mark the bounding box by a rectangle
  16. ax.add_patch(plt.Rectangle((.2, .4), .6, .5, ls="--", ec="c", fc="None",
  17. transform=ax.transAxes))
  18. # We set the axis limits to something other than the default, in order to not
  19. # distract from the fact that axes coodinates are used here.
  20. ax.axis([0, 10, 0, 10])
  21. # Note how the two following insets are created at the same positions, one by
  22. # use of the default parent axes' bbox and the other via a bbox in axes
  23. # coordinates and the respective transform.
  24. ax2 = fig.add_subplot(222)
  25. axins2 = inset_axes(ax2, width="30%", height="50%")
  26. ax3 = fig.add_subplot(224)
  27. axins3 = inset_axes(ax3, width="100%", height="100%",
  28. bbox_to_anchor=(.7, .5, .3, .5),
  29. bbox_transform=ax3.transAxes)
  30. # For visualization purposes we mark the bounding box by a rectangle
  31. ax2.add_patch(plt.Rectangle((0, 0), 1, 1, ls="--", lw=2, ec="c", fc="None"))
  32. ax3.add_patch(plt.Rectangle((.7, .5), .3, .5, ls="--", lw=2,
  33. ec="c", fc="None"))
  34. # Turn ticklabels off
  35. for axi in [axins2, axins3, ax2, ax3]:
  36. axi.tick_params(labelleft=False, labelbottom=False)
  37. plt.show()

插入定位器演示2

在上述方法中,使用了轴变换和4元组边界框,因为它主要用于指定相对于其所插入的轴的插入值。但是,其他用例也是可能的。下面的示例检查其中一些。

  1. fig = plt.figure(figsize=[5.5, 2.8])
  2. ax = fig.add_subplot(131)
  3. # Create an inset outside the axes
  4. axins = inset_axes(ax, width="100%", height="100%",
  5. bbox_to_anchor=(1.05, .6, .5, .4),
  6. bbox_transform=ax.transAxes, loc=2, borderpad=0)
  7. axins.tick_params(left=False, right=True, labelleft=False, labelright=True)
  8. # Create an inset with a 2-tuple bounding box. Note that this creates a
  9. # bbox without extent. This hence only makes sense when specifying
  10. # width and height in absolute units (inches).
  11. axins2 = inset_axes(ax, width=0.5, height=0.4,
  12. bbox_to_anchor=(0.33, 0.25),
  13. bbox_transform=ax.transAxes, loc=3, borderpad=0)
  14. ax2 = fig.add_subplot(133)
  15. ax2.set_xscale("log")
  16. ax2.axis([1e-6, 1e6, -2, 6])
  17. # Create inset in data coordinates using ax.transData as transform
  18. axins3 = inset_axes(ax2, width="100%", height="100%",
  19. bbox_to_anchor=(1e-2, 2, 1e3, 3),
  20. bbox_transform=ax2.transData, loc=2, borderpad=0)
  21. # Create an inset horizontally centered in figure coordinates and vertically
  22. # bound to line up with the axes.
  23. from matplotlib.transforms import blended_transform_factory
  24. transform = blended_transform_factory(fig.transFigure, ax2.transAxes)
  25. axins4 = inset_axes(ax2, width="16%", height="34%",
  26. bbox_to_anchor=(0, 0, 1, 1),
  27. bbox_transform=transform, loc=8, borderpad=0)
  28. plt.show()

插入定位器演示3

下载这个示例