diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index b66809c1d491..5950bb24ce73 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 self.get_array().size == 0: + 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..27c316897b1c 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,15 @@ 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() + + nose.tools.assert_raises(RuntimeError, im.make_image, fig._cachedRenderer) + + if __name__ == '__main__': nose.runmodule(argv=['-s', '--with-doctest'], exit=False)