1"""
2==================
3Inset Locator Demo
4==================
5
6"""
7
8###############################################################################
9# The `.inset_locator`'s `~.inset_locator.inset_axes` allows
10# easily placing insets in the corners of the axes by specifying a width and
11# height and optionally a location (loc) that accepts locations as codes,
12# similar to `~matplotlib.axes.Axes.legend`.
13# By default, the inset is offset by some points from the axes,
14# controlled via the *borderpad* parameter.
15
16import matplotlib.pyplot as plt
17from mpl_toolkits.axes_grid1.inset_locator import inset_axes
18
19
20fig, (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
24axins = 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)
28axins2 = 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)
33axins3 = 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
37axins4 = inset_axes(ax2, width="20%", height="20%", loc=4, borderpad=1)
38
39# Turn ticklabels of insets off
40for axi in [axins, axins2, axins3, axins4]:
41    axi.tick_params(labelleft=False, labelbottom=False)
42
43plt.show()
44
45
46###############################################################################
47# The parameters *bbox_to_anchor* and *bbox_transform* 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#
53
54fig = plt.figure(figsize=[5.5, 2.8])
55ax = fig.add_subplot(121)
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
67axins = 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
72ax.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 coordinates are used here.
77ax.set(xlim=(0, 10), ylim=(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.
83ax2 = fig.add_subplot(222)
84axins2 = inset_axes(ax2, width="30%", height="50%")
85
86ax3 = fig.add_subplot(224)
87axins3 = 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
92ax2.add_patch(plt.Rectangle((0, 0), 1, 1, ls="--", lw=2, ec="c", fc="None"))
93ax3.add_patch(plt.Rectangle((.7, .5), .3, .5, ls="--", lw=2,
94                            ec="c", fc="None"))
95
96# Turn ticklabels off
97for axi in [axins2, axins3, ax2, ax3]:
98    axi.tick_params(labelleft=False, labelbottom=False)
99
100plt.show()
101
102
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#
109
110fig = plt.figure(figsize=[5.5, 2.8])
111ax = fig.add_subplot(131)
112
113# Create an inset outside the axes
114axins = 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)
117axins.tick_params(left=False, right=True, labelleft=False, labelright=True)
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).
122axins2 = 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)
125
126
127ax2 = fig.add_subplot(133)
128ax2.set_xscale("log")
129ax2.set(xlim=(1e-6, 1e6), ylim=(-2, 6))
130
131# Create inset in data coordinates using ax.transData as transform
132axins3 = 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)
135
136# Create an inset horizontally centered in figure coordinates and vertically
137# bound to line up with the axes.
138from matplotlib.transforms import blended_transform_factory
139transform = blended_transform_factory(fig.transFigure, ax2.transAxes)
140axins4 = inset_axes(ax2, width="16%", height="34%",
141                    bbox_to_anchor=(0, 0, 1, 1),
142                    bbox_transform=transform, loc=8, borderpad=0)
143
144plt.show()
145