From c4700bb24666394637a8d870b9a74622ca438eef Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 1 Feb 2017 00:04:23 -0500 Subject: [PATCH 1/3] FIX: do not try to render empty images Do not bother trying to render Images which have zero-dimension in any direction. Simply return from the `draw` method early closes #7886 --- lib/matplotlib/image.py | 12 ++++++++++++ lib/matplotlib/tests/test_image.py | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index b66809c1d491..2e1be749c89d 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -301,6 +301,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, if A is None: raise RuntimeError('You must first set the image' ' array or the image attribute') + if any(s == 0 for s in A.shape): + raise RuntimeError("_make_image must get a non-empty image. " + "Your Artist's draw method must filter before " + "this method is called.") clipped_bbox = Bbox.intersection(out_bbox, clip_bbox) @@ -478,9 +482,17 @@ def _check_unsampled_image(self, renderer): @allow_rasterization def draw(self, renderer, *args, **kwargs): + # if not visible, declare victory and return if not self.get_visible(): + self.stale = False return + # for empty images, there is nothing to draw! + if any(s == 0 for s in self.get_size()): + self.stale = False + return + + # actually render the image. gc = renderer.new_gc() self._set_gc_clip(gc) gc.set_alpha(self.get_alpha()) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 89540aa2bcac..74ddccb91708 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -32,6 +32,7 @@ import numpy as np import nose +import nose.tools try: from PIL import Image @@ -765,5 +766,16 @@ def test_imshow_no_warn_invalid(): assert len(warns) == 0 +@cleanup +def test_empty_imshow(): + fig, ax = plt.subplots() + im = ax.imshow([[]]) + im.set_extent([-5, 5, -5, 5]) + fig.canvas.draw() + + with nose.tools.raises(RuntimeError): + im.make_image(fig._cachedRenderer) + + if __name__ == '__main__': nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From 48d0ddcfc55d235698e92fe8f34643bdd0684cfb Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 4 Feb 2017 20:31:44 -0500 Subject: [PATCH 2/3] TST: fix test (turns out it is not a context manager) --- lib/matplotlib/tests/test_image.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 74ddccb91708..27c316897b1c 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -773,8 +773,7 @@ def test_empty_imshow(): im.set_extent([-5, 5, -5, 5]) fig.canvas.draw() - with nose.tools.raises(RuntimeError): - im.make_image(fig._cachedRenderer) + nose.tools.assert_raises(RuntimeError, im.make_image, fig._cachedRenderer) if __name__ == '__main__': From babdf736229b854379cdd1eff92d184d2c0e2dcd Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 6 Feb 2017 15:30:57 -0500 Subject: [PATCH 3/3] MNT: minor tweak to checking size --- lib/matplotlib/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 2e1be749c89d..5950bb24ce73 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -488,7 +488,7 @@ def draw(self, renderer, *args, **kwargs): return # for empty images, there is nothing to draw! - if any(s == 0 for s in self.get_size()): + if self.get_array().size == 0: self.stale = False return