|
4 | 4 | ==================
|
5 | 5 |
|
6 | 6 | """
|
| 7 | + |
| 8 | +############################################################################### |
| 9 | +# The `.inset_locator`'s `~.inset_axes` allows to easily place insets in the |
| 10 | +# corners of the axes by specifying a width and height and optionally |
| 11 | +# a location (loc) which accepts locations as codes, similar to |
| 12 | +# `~matplotlib.axes.Axes.legend`. |
| 13 | +# By default, the inset is offset by some points from the axes - this is |
| 14 | +# controlled via the `borderpad` parameter. |
| 15 | + |
7 | 16 | import matplotlib.pyplot as plt
|
| 17 | +from mpl_toolkits.axes_grid1.inset_locator import inset_axes |
| 18 | + |
| 19 | + |
| 20 | +fig, (ax, ax2) = plt.subplots(1, 2, figsize=[5.5, 2.8]) |
| 21 | + |
| 22 | +# Create inset of width 1.3 inches and height 0.9 inches |
| 23 | +# at the default upper right location |
| 24 | +axins = inset_axes(ax, width=1.3, height=0.9) |
| 25 | + |
| 26 | +# Create inset of width 30% and height 40% of the parent axes' bounding box |
| 27 | +# at the lower left corner (loc=3) |
| 28 | +axins2 = inset_axes(ax, width="30%", height="40%", loc=3) |
| 29 | + |
| 30 | +# Create inset of mixed specifications in the second subplot; |
| 31 | +# width is 30% of parent axes' bounding box and |
| 32 | +# height is 1 inch at the upper left corner (loc=2) |
| 33 | +axins3 = inset_axes(ax2, width="30%", height=1., loc=2) |
| 34 | + |
| 35 | +# Create an inset in the lower right corner (loc=4) with borderpad=1, i.e. |
| 36 | +# 10 points padding (as 10pt is the default fontsize) to the parent axes |
| 37 | +axins4 = inset_axes(ax2, width="20%", height="20%", loc=4, borderpad=1) |
| 38 | + |
| 39 | +# Turn ticklabels of insets off |
| 40 | +for axi in [axins, axins2, axins3, axins4]: |
| 41 | + axi.tick_params(labelleft=False, labelbottom=False) |
| 42 | + |
| 43 | +plt.show() |
8 | 44 |
|
9 |
| -from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes |
10 |
| -from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar |
11 | 45 |
|
| 46 | +############################################################################### |
| 47 | +# The arguments `bbox_to_anchor` and `bbox_transfrom` can be used for a more |
| 48 | +# fine grained control over the inset position and size or even to position |
| 49 | +# the inset at completely arbitrary positions. |
| 50 | +# The `bbox_to_anchor` sets the bounding box in coordinates according to the |
| 51 | +# `bbox_transform`. |
| 52 | +# |
12 | 53 |
|
13 |
| -def add_sizebar(ax, size): |
14 |
| - asb = AnchoredSizeBar(ax.transData, |
15 |
| - size, |
16 |
| - str(size), |
17 |
| - loc=8, |
18 |
| - pad=0.1, borderpad=0.5, sep=5, |
19 |
| - frameon=False) |
20 |
| - ax.add_artist(asb) |
| 54 | +fig = plt.figure(figsize=[5.5, 2.8]) |
| 55 | +ax = fig.add_subplot(121) |
21 | 56 |
|
| 57 | +# We use the axes transform as bbox_transform. Therefore the bounding box |
| 58 | +# needs to be specified in axes coordinates ((0,0) is the lower left corner |
| 59 | +# of the axes, (1,1) is the upper right corner). |
| 60 | +# The bounding box (.2, .4, .6, .5) starts at (.2,.4) and ranges to (.8,.9) |
| 61 | +# in those coordinates. |
| 62 | +# Inside of this bounding box an inset of half the bounding box' width and |
| 63 | +# three quarters of the bounding box' height is created. The lower left corner |
| 64 | +# of the inset is aligned to the lower left corner of the bounding box (loc=3). |
| 65 | +# The inset is then offset by the default 0.5 in units of the font size. |
| 66 | + |
| 67 | +axins = inset_axes(ax, width="50%", height="75%", |
| 68 | + bbox_to_anchor=(.2, .4, .6, .5), |
| 69 | + bbox_transform=ax.transAxes, loc=3) |
| 70 | + |
| 71 | +# For visualization purposes we mark the bounding box by a rectangle |
| 72 | +ax.add_patch(plt.Rectangle((.2, .4), .6, .5, ls="--", ec="c", fc="None", |
| 73 | + transform=ax.transAxes)) |
| 74 | + |
| 75 | +# We set the axis limits to something other than the default, in order to not |
| 76 | +# distract from the fact that axes coodinates are used here. |
| 77 | +ax.axis([0, 10, 0, 10]) |
| 78 | + |
| 79 | + |
| 80 | +# Note how the two following insets are created at the same positions, one by |
| 81 | +# use of the default parent axes' bbox and the other via a bbox in axes |
| 82 | +# coordinates and the respective transform. |
| 83 | +ax2 = fig.add_subplot(222) |
| 84 | +axins2 = inset_axes(ax2, width="30%", height="50%") |
| 85 | + |
| 86 | +ax3 = fig.add_subplot(224) |
| 87 | +axins3 = inset_axes(ax3, width="100%", height="100%", |
| 88 | + bbox_to_anchor=(.7, .5, .3, .5), |
| 89 | + bbox_transform=ax3.transAxes) |
| 90 | + |
| 91 | +# For visualization purposes we mark the bounding box by a rectangle |
| 92 | +ax2.add_patch(plt.Rectangle((0, 0), 1, 1, ls="--", lw=2, ec="c", fc="None")) |
| 93 | +ax3.add_patch(plt.Rectangle((.7, .5), .3, .5, ls="--", lw=2, |
| 94 | + ec="c", fc="None")) |
| 95 | + |
| 96 | +# Turn ticklabels off |
| 97 | +for axi in [axins2, axins3, ax2, ax3]: |
| 98 | + axi.tick_params(labelleft=False, labelbottom=False) |
| 99 | + |
| 100 | +plt.show() |
22 | 101 |
|
23 |
| -fig, (ax, ax2) = plt.subplots(1, 2, figsize=[5.5, 3]) |
24 | 102 |
|
25 |
| -# first subplot |
26 |
| -ax.set_aspect(1) |
| 103 | +############################################################################### |
| 104 | +# In the above the axes transform together with 4-tuple bounding boxes has been |
| 105 | +# used as it mostly is useful to specify an inset relative to the axes it is |
| 106 | +# an inset to. However other use cases are equally possible. The following |
| 107 | +# example examines some of those. |
| 108 | +# |
27 | 109 |
|
28 |
| -axins = inset_axes(ax, |
29 |
| - width="30%", # width = 30% of parent_bbox |
30 |
| - height=1., # height : 1 inch |
31 |
| - loc=3) |
| 110 | +fig = plt.figure(figsize=[5.5, 2.8]) |
| 111 | +ax = fig.add_subplot(131) |
32 | 112 |
|
33 |
| -plt.xticks(visible=False) |
34 |
| -plt.yticks(visible=False) |
| 113 | +# Create an inset outside the axes |
| 114 | +axins = inset_axes(ax, width="100%", height="100%", |
| 115 | + bbox_to_anchor=(1.05, .6, .5, .4), |
| 116 | + bbox_transform=ax.transAxes, loc=2, borderpad=0) |
| 117 | +axins.tick_params(left=False, right=True, labelleft=False, labelright=True) |
35 | 118 |
|
| 119 | +# Create an inset with a 2-tuple bounding box. Note that this creates a |
| 120 | +# bbox without extent. This hence only makes sense when specifying |
| 121 | +# width and height in absolute units (inches). |
| 122 | +axins2 = inset_axes(ax, width=0.5, height=0.4, |
| 123 | + bbox_to_anchor=(0.33, 0.25), |
| 124 | + bbox_transform=ax.transAxes, loc=3, borderpad=0) |
36 | 125 |
|
37 |
| -# second subplot |
38 |
| -ax2.set_aspect(1) |
39 | 126 |
|
40 |
| -axins = zoomed_inset_axes(ax2, zoom=0.5, loc='upper right') |
41 |
| -# fix the number of ticks on the inset axes |
42 |
| -axins.yaxis.get_major_locator().set_params(nbins=7) |
43 |
| -axins.xaxis.get_major_locator().set_params(nbins=7) |
| 127 | +ax2 = fig.add_subplot(133) |
| 128 | +ax2.set_xscale("log") |
| 129 | +ax2.axis([1e-6, 1e6, -2, 6]) |
44 | 130 |
|
45 |
| -plt.xticks(visible=False) |
46 |
| -plt.yticks(visible=False) |
| 131 | +# Create inset in data coordinates using ax.transData as transform |
| 132 | +axins3 = inset_axes(ax2, width="100%", height="100%", |
| 133 | + bbox_to_anchor=(1e-2, 2, 1e3, 3), |
| 134 | + bbox_transform=ax2.transData, loc=2, borderpad=0) |
47 | 135 |
|
48 |
| -add_sizebar(ax2, 0.5) |
49 |
| -add_sizebar(axins, 0.5) |
| 136 | +# Create an inset horizontally centered in figure coordinates and vertically |
| 137 | +# bound to line up with the axes. |
| 138 | +from matplotlib.transforms import blended_transform_factory |
| 139 | +transform = blended_transform_factory(fig.transFigure, ax2.transAxes) |
| 140 | +axins4 = inset_axes(ax2, width="16%", height="34%", |
| 141 | + bbox_to_anchor=(0, 0, 1, 1), |
| 142 | + bbox_transform=transform, loc=8, borderpad=0) |
50 | 143 |
|
51 |
| -plt.draw() |
52 | 144 | plt.show()
|
0 commit comments