From 488eb61e5208454d05a68f952644ec1c7d1f6342 Mon Sep 17 00:00:00 2001 From: Maximilian Albert Date: Sat, 29 Nov 2014 15:37:01 +0000 Subject: [PATCH 1/3] Added missing implementation of get_window_extent for AxisImage and test (fixes #2980). --- lib/matplotlib/image.py | 4 ++++ lib/matplotlib/tests/test_image.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 65fb94f4393a..76a6709edd79 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -574,6 +574,10 @@ def __init__(self, ax, **kwargs ) + def get_window_extent(self, renderer=None): + x0, x1, y0, y1 = self._extent + return Bbox.from_extents([x0, y0, x1, y1]).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..91b265da336c 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.22, 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, 220], [700, 900]]) + + if __name__=='__main__': import nose nose.runmodule(argv=['-s','--with-doctest'], exit=False) From 02203517ed6b52584fd44340e693b952626dbe47 Mon Sep 17 00:00:00 2001 From: Maximilian Albert Date: Sat, 29 Nov 2014 16:56:51 +0000 Subject: [PATCH 2/3] Reduce linewidth for PEP-8 compliance. --- lib/matplotlib/tests/test_image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 91b265da336c..4d80b16fbd55 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -348,13 +348,13 @@ def test_get_window_extent_for_AxisImage(): 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.22, 0.9], interpolation='nearest') + 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, 220], [700, 900]]) + assert_array_equal(im_bbox.get_points(), [[400, 200], [700, 900]]) if __name__=='__main__': From 87bac9dd3159dd7f35667cfaeacfe7dc2e5cc0cd Mon Sep 17 00:00:00 2001 From: Maximilian Albert Date: Sun, 30 Nov 2014 10:53:44 +0000 Subject: [PATCH 3/3] Split long line. --- lib/matplotlib/image.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 76a6709edd79..17760b7547dc 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -576,7 +576,8 @@ def __init__(self, ax, def get_window_extent(self, renderer=None): x0, x1, y0, y1 = self._extent - return Bbox.from_extents([x0, y0, x1, y1]).transformed(self.axes.transData) + 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: