8000 Added missing implementation of get_window_extent for AxisImage and t… · luoq/matplotlib@488eb61 · GitHub
[go: up one dir, main page]

Skip to content

Commit 488eb61

Browse files
committed
Added missing implementation of get_window_extent for AxisImage and test (fixes matplotlib#2980).
1 parent df3530d commit 488eb61

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/matplotlib/image.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ def __init__(self, ax,
574574
**kwargs
575575
)
576576

577+
def get_window_extent(self, renderer=None):
578+
x0, x1, y0, y1 = self._extent
579+
return Bbox.from_extents([x0, y0, x1, y1]).transformed(self.axes.transData)
580+
577581
def make_image(self, magnification=1.0):
578582
if self._A is None:
579583
raise RuntimeError('You must first set the image'

lib/matplotlib/tests/test_image.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,28 @@ def test_bbox_image_inverted():
335335
axes.add_artist(bbox_im)
336336

337337

338+
@cleanup
339+
def test_get_window_extent_for_AxisImage():
340+
# Create a figure of known size (1000x1000 pixels), place an image
341+
# object at a given location and check that get_window_extent()
342+
# returns the correct bounding box values (in pixels).
343+
344+
im = np.array([[0.25, 0.75, 1.0, 0.75], [0.1, 0.65, 0.5, 0.4], \
345+
[0.6, 0.3, 0.0, 0.2], [0.7, 0.9, 0.4, 0.6]])
346+
fig = plt.figure(figsize=(10, 10), dpi=100)
347+
ax = plt.subplot()
348+
ax.set_position([0, 0, 1, 1])
349+
ax.set_xlim(0, 1)
350+
ax.set_ylim(0, 1)
351+
im_obj = ax.imshow(im, extent=[0.4, 0.7, 0.22, 0.9], interpolation='nearest')
352+
353+
fig.canvas.draw()
354+
renderer = fig.canvas.renderer
355+
im_bbox = im_obj.get_window_extent(renderer)
356+
357+
assert_array_equal(im_bbox.get_points(), [[400, 220], [700, 900]])
358+
359+
338360
if __name__=='__main__':
339361
import nose
340362
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0