diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 4a931a368012..6437953fef9a 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -879,10 +879,10 @@ def __call__(self, value, clip=None): self.autoscale_None(result) vmin, vmax = self.vmin, self.vmax - if vmin > vmax: - raise ValueError("minvalue must be less than or equal to maxvalue") - elif vmin == vmax: + if vmin == vmax: result.fill(0) # Or should it be all masked? Or 0.5? + elif vmin > vmax: + raise ValueError("minvalue must be less than or equal to maxvalue") else: vmin = float(vmin) vmax = float(vmax) @@ -920,9 +920,9 @@ def autoscale(self, A): def autoscale_None(self, A): ' autoscale only None-valued vmin or vmax' - if self.vmin is None: + if self.vmin is None and np.size(A) > 0: self.vmin = ma.min(A) - if self.vmax is None: + if self.vmax is None and np.size(A) > 0: self.vmax = ma.max(A) def scaled(self): diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 445d0f6960c9..bdd26e1f26dd 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -7,6 +7,9 @@ from numpy.testing.utils import assert_array_equal import matplotlib.colors as mcolors import matplotlib.cm as cm +import matplotlib.pyplot as plt +from matplotlib.testing.decorators import cleanup + def test_colormap_endian(): """ @@ -23,6 +26,7 @@ def test_colormap_endian(): #print(anative.dtype.isnative, aforeign.dtype.isnative) assert_array_equal(cmap(anative), cmap(aforeign)) + def test_BoundaryNorm(): """ Github issue #1258: interpolation was failing with numpy @@ -36,6 +40,7 @@ def test_BoundaryNorm(): ncolors = len(boundaries) bn = mcolors.BoundaryNorm(boundaries, ncolors) assert_array_equal(bn(vals), expected) + def test_LogNorm(): """ @@ -46,3 +51,10 @@ def test_LogNorm(): ln = mcolors.LogNorm(clip=True, vmax=5) assert_array_equal(ln([1, 6]), [0, 1.0]) + +@cleanup +def test_autoscale_masked(): + # Test for #2336. Previously fully masked data would trigger a ValueError. + data = np.ma.masked_all((12, 20)) + plt.pcolor(data) + plt.draw()