diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 65fb94f4393a..17760b7547dc 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -574,6 +574,11 @@ def __init__(self, ax, **kwargs ) + def get_window_extent(self, renderer=None): + x0, x1, y0, y1 = self._extent + bbox = Bbox.from_extents([x0, y0, x1, y1]) + return bbox.transformed(self.axes.transData) + def make_image(self, magnification=1.0): if self._A is None: raise RuntimeError('You must first set the image' diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 01f342051666..4d80b16fbd55 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -335,6 +335,28 @@ def test_bbox_image_inverted(): axes.add_artist(bbox_im) +@cleanup +def test_get_window_extent_for_AxisImage(): + # Create a figure of known size (1000x1000 pixels), place an image + # object at a given location and check that get_window_extent() + # returns the correct bounding box values (in pixels). + + im = np.array([[0.25, 0.75, 1.0, 0.75], [0.1, 0.65, 0.5, 0.4], \ + [0.6, 0.3, 0.0, 0.2], [0.7, 0.9, 0.4, 0.6]]) + fig = plt.figure(figsize=(10, 10), dpi=100) + ax = plt.subplot() + ax.set_position([0, 0, 1, 1]) + ax.set_xlim(0, 1) + ax.set_ylim(0, 1) + im_obj = ax.imshow(im, extent=[0.4, 0.7, 0.2, 0.9], interpolation='nearest') + + fig.canvas.draw() + renderer = fig.canvas.renderer + im_bbox = im_obj.get_window_extent(renderer) + + assert_array_equal(im_bbox.get_points(), [[400, 200], [700, 900]]) + + if __name__=='__main__': import nose nose.runmodule(argv=['-s','--with-doctest'], exit=False)