Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3225451

Browse filesBrowse files
Fix inset_axes + doc
1 parent 213f399 commit 3225451
Copy full SHA for 3225451

File tree

Expand file treeCollapse file tree

4 files changed

+293
-74
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+293
-74
lines changed

‎examples/axes_grid1/inset_locator_demo.py

Copy file name to clipboardExpand all lines: examples/axes_grid1/inset_locator_demo.py
+122-30Lines changed: 122 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,141 @@
44
==================
55
66
"""
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+
716
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()
844

9-
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
10-
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
1145

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+
#
1253

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)
2156

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()
22101

23-
fig, (ax, ax2) = plt.subplots(1, 2, figsize=[5.5, 3])
24102

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+
#
27109

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)
32112

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)
35118

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)
36125

37-
# second subplot
38-
ax2.set_aspect(1)
39126

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])
44130

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)
47135

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)
50143

51-
plt.draw()
52144
plt.show()

‎examples/axes_grid1/inset_locator_demo2.py

Copy file name to clipboardExpand all lines: examples/axes_grid1/inset_locator_demo2.py
+46-15Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
Inset Locator Demo2
44
===================
55
6+
This Demo shows how to create a zoomed inset via `~.zoomed_inset_axes`.
7+
In the first subplot an `~.AnchoredSizeBar` shows the zoom effect.
8+
In the second subplot a connection to the region of interest is
9+
created via `~.mark_inset`.
610
"""
11+
712
import matplotlib.pyplot as plt
813

9-
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
10-
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
14+
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
15+
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
1116

1217
import numpy as np
1318

@@ -20,36 +25,62 @@ def get_demo_image():
2025
# z is a numpy array of 15x15
2126
return z, (-3, 4, -4, 3)
2227

23-
fig, ax = plt.subplots(figsize=[5, 4])
28+
fig, (ax, ax2) = plt.subplots(ncols=2, figsize=[6, 3])
29+
30+
31+
# First subplot, showing an inset with a size bar.
32+
ax.set_aspect(1)
33+
34+
axins = zoomed_inset_axes(ax, zoom=0.5, loc='upper right')
35+
# fix the number of ticks on the inset axes
36+
axins.yaxis.get_major_locator().set_params(nbins=7)
37+
axins.xaxis.get_major_locator().set_params(nbins=7)
38+
39+
plt.setp(axins.get_xticklabels(), visible=False)
40+
plt.setp(axins.get_yticklabels(), visible=False)
41+
42+
43+
def add_sizebar(ax, size):
44+
asb = AnchoredSizeBar(ax.transData,
45+
size,
46+
str(size),
47+
loc=8,
48+
pad=0.1, borderpad=0.5, sep=5,
49+
frameon=False)
50+
ax.add_artist(asb)
51+
52+
add_sizebar(ax, 0.5)
53+
add_sizebar(axins, 0.5)
2454

25-
# prepare the demo image
55+
56+
# Second subplot, showing an image with an inset zoom
57+
# and a marked inset
2658
Z, extent = get_demo_image()
2759
Z2 = np.zeros([150, 150], dtype="d")
2860
ny, nx = Z.shape
2961
Z2[30:30 + ny, 30:30 + nx] = Z
3062

3163
# extent = [-3, 4, -4, 3]
32-
ax.imshow(Z2, extent=extent, interpolation="nearest",
64+
ax2.imshow(Z2, extent=extent, interpolation="nearest",
3365
origin="lower")
3466

35-
axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6
36-
axins.imshow(Z2, extent=extent, interpolation="nearest",
67+
axins2 = zoomed_inset_axes(ax2, 6, loc=1) # zoom = 6
68+
axins2.imshow(Z2, extent=extent, interpolation="nearest",
3769
origin="lower")
3870

3971
# sub region of the original image
4072
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
41-
axins.set_xlim(x1, x2)
42-
axins.set_ylim(y1, y2)
73+
axins2.set_xlim(x1, x2)
74+
axins2.set_ylim(y1, y2)
4375
# fix the number of ticks on the inset axes
44-
axins.yaxis.get_major_locator().set_params(nbins=7)
45-
axins.xaxis.get_major_locator().set_params(nbins=7)
76+
axins2.yaxis.get_major_locator().set_params(nbins=7)
77+
axins2.xaxis.get_major_locator().set_params(nbins=7)
4678

47-
plt.xticks(visible=False)
48-
plt.yticks(visible=False)
79+
plt.setp(axins2.get_xticklabels(), visible=False)
80+
plt.setp(axins2.get_yticklabels(), visible=False)
4981

5082
# draw a bbox of the region of the inset axes in the parent axes and
5183
# connecting lines between the bbox and the inset axes area
52-
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
84+
mark_inset(ax2, axins2, loc1=2, loc2=4, fc="none", ec="0.5")
5385

54-
plt.draw()
5586
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.