Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d6632e

Browse files
authored
Merge pull request #10756 from AlexCav/iss-8120-bugfix
Fixes png showing inconsistent inset_axes position
2 parents 1707b1e + 6b5537f commit 4d6632e

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

lib/mpl_toolkits/axes_grid1/inset_locator.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def __call__(self, ax, renderer):
9090
class AnchoredSizeLocator(AnchoredLocatorBase):
9191
def __init__(self, bbox_to_anchor, x_size, y_size, loc,
9292
borderpad=0.5, bbox_transform=None):
93-
9493
super().__init__(
9594
bbox_to_anchor, None, loc,
9695
borderpad=borderpad, bbox_transform=bbox_transform
@@ -105,24 +104,23 @@ def get_extent(self, renderer):
105104
dpi = renderer.points_to_pixels(72.)
106105

107106
r, a = self.x_size.get_size(renderer)
108-
width = w*r + a*dpi
107+
width = w * r + a * dpi
109108

110109
r, a = self.y_size.get_size(renderer)
111-
height = h*r + a*dpi
110+
height = h * r + a * dpi
112111
xd, yd = 0, 0
113112

114113
fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
115114
pad = self.pad * fontsize
116115

117-
return width+2*pad, height+2*pad, xd+pad, yd+pad
116+
return width + 2 * pad, height + 2 * pad, xd + pad, yd + pad
118117

119118

120119
class AnchoredZoomLocator(AnchoredLocatorBase):
121120
def __init__(self, parent_axes, zoom, loc,
122121
borderpad=0.5,
123122
bbox_to_anchor=None,
124123
bbox_transform=None):
125-
126124
self.parent_axes = parent_axes
127125
self.zoom = zoom
128126

@@ -141,7 +139,7 @@ def get_extent(self, renderer):
141139
fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
142140
pad = self.pad * fontsize
143141

144-
return abs(w*self.zoom)+2*pad, abs(h*self.zoom)+2*pad, pad, pad
142+
return abs(w * self.zoom) + 2 * pad, abs(h * self.zoom) + 2 * pad, pad, pad
145143

146144

147145
class BboxPatch(Patch):
@@ -184,6 +182,7 @@ def get_path(self):
184182
Path.CLOSEPOLY]
185183

186184
return Path(verts, codes)
185+
187186
get_path.__doc__ = Patch.get_path.__doc__
188187

189188

@@ -318,6 +317,7 @@ def __init__(self, bbox1, bbox2, loc1, loc2=None, **kwargs):
318317
def get_path(self):
319318
return self.connect_bbox(self.bbox1, self.bbox2,
320319
self.loc1, self.loc2)
320+
321321
get_path.__doc__ = Patch.get_path.__doc__
322322

323323

@@ -373,6 +373,7 @@ def get_path(self):
373373
list(path2.vertices) +
374374
[path1.vertices[0]])
375375
return Path(path_merged)
376+
376377
get_path.__doc__ = BboxConnector.get_path.__doc__
377378

378379

@@ -453,6 +454,9 @@ def inset_axes(parent_axes, width, height, loc=1,
453454
if bbox_to_anchor is None:
454455
bbox_to_anchor = parent_axes.bbox
455456

457+
if bbox_transform is None:
458+
bbox_transform = parent_axes.transAxes
459+
456460
axes_locator = AnchoredSizeLocator(bbox_to_anchor,
457461
width, height,
458462
loc=loc,

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
from mpl_toolkits.axes_grid1 import host_subplot
1010
from mpl_toolkits.axes_grid1 import make_axes_locatable
1111
from mpl_toolkits.axes_grid1 import AxesGrid
12-
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
12+
from mpl_toolkits.axes_grid1.inset_locator import (
13+
zoomed_inset_axes,
14+
mark_inset,
15+
inset_axes
16+
)
1317
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
1418

1519
from matplotlib.colors import LogNorm
@@ -155,6 +159,73 @@ def get_demo_image():
155159
ax.add_artist(asb)
156160

157161

162+
@image_comparison(
163+
baseline_images=['inset_axes'], style='default', extensions=['png'],
164+
remove_text=True)
165+
def test_inset_axes():
166+
def get_demo_image():
167+
from matplotlib.cbook import get_sample_data
168+
import numpy as np
169+
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
170+
z = np.load(f)
171+
# z is a numpy array of 15x15
172+
return z, (-3, 4, -4, 3)
173+
174+
fig, ax = plt.subplots(figsize=[5, 4])
175+
176+
# prepare the demo image
177+
Z, extent = get_demo_image()
178+
Z2 = np.zeros([150, 150], dtype="d")
179+
ny, nx = Z.shape
180+
Z2[30:30 + ny, 30:30 + nx] = Z
181+
182+
# extent = [-3, 4, -4, 3]
183+
ax.imshow(Z2, extent=extent, interpolation="nearest",
184+
origin="lower")
185+
186+
# creating our inset axes without a bbox_transform parameter
187+
axins = inset_axes(ax, width=1., height=1., bbox_to_anchor=(1, 1))
188+
189+
axins.imshow(Z2, extent=extent, interpolation="nearest",
190+
origin="lower")
191+
axins.yaxis.get_major_locator().set_params(nbins=7)
192+
axins.xaxis.get_major_locator().set_params(nbins=7)
193+
# sub region of the original image
194+
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
195+
axins.set_xlim(x1, x2)
196+
axins.set_ylim(y1, y2)
197+
198+
plt.xticks(visible=False)
199+
plt.yticks(visible=False)
200+
201+
# draw a bbox of the region of the inset axes in the parent axes and
202+
# connecting lines between the bbox and the inset axes area
203+
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
204+
205+
asb = AnchoredSizeBar(ax.transData,
206+
0.5,
207+
'0.5',
208+
loc=8,
209+
pad=0.1, borderpad=0.5, sep=5,
210+
frameon=False)
211+
ax.add_artist(asb)
212+
213+
214+
def test_inset_axes_without_transform_should_use_parent_axes():
215+
# creating our figure
216+
fig = plt.figure(dpi=150)
217+
218+
# gca method gets current axes of the figure
219+
ax = plt.gca()
220+
ax.plot([0.0, 0.25, 0.50, 1.0], [0.1, 0.2, 0.4, 0.9], color='b')
221+
222+
# creating our inset_axes. without a bbox_transform parameter
223+
ax_ins = inset_axes(ax, width=1., height=1., bbox_to_anchor=(1, 1))
224+
ax_ins.plot([0.0, 0.25, 0.50, 1.0], [0.9, 0.4, 0.2, 0.1], color='r')
225+
226+
assert ax.transAxes == ax_ins.transAxes
227+
228+
158229
@image_comparison(baseline_images=['zoomed_axes',
159230
'inverted_zoomed_axes'],
160231
extensions=['png'])

0 commit comments

Comments
 (0)