diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 7c5b1f04ae..3caf386ef6 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -62,6 +62,10 @@ This document explains the changes made to Iris for this release #. `@gcaria`_ fixed :meth:`~iris.cube.Cube.ancillary_variable_dims` to also accept the string name of a :class:`~iris.coords.AncillaryVariable`. (:pull:`3931`) +#. `@rcomer`_ modified :func:`~iris.plot.contourf` to skip the special handling for + antialiasing when data values are too low for it to have an effect. This caused + unexpected artifacts in some edge cases, as shown at :issue:`4086`. (:pull:`4150`) + 💣 Incompatible Changes ======================= diff --git a/lib/iris/plot.py b/lib/iris/plot.py index cc06c98afc..3cf0040035 100644 --- a/lib/iris/plot.py +++ b/lib/iris/plot.py @@ -1082,7 +1082,7 @@ def contourf(cube, *args, **kwargs): colors = colors[:-1] else: colors = colors[:-1] - if len(levels) > 0: + if len(levels) > 0 and cube.data.max() > levels[0]: # Draw the lines just *below* the polygons to ensure we minimise # any boundary shift. zorder = result.collections[0].zorder - 0.1 diff --git a/lib/iris/tests/unit/plot/test_contourf.py b/lib/iris/tests/unit/plot/test_contourf.py index 0429b94c47..78f138c0da 100644 --- a/lib/iris/tests/unit/plot/test_contourf.py +++ b/lib/iris/tests/unit/plot/test_contourf.py @@ -12,6 +12,8 @@ from unittest import mock import numpy as np +import matplotlib +import matplotlib.pyplot as plt from iris.tests.stock import simple_2d from iris.tests.unit.plot import TestGraphicStringCoord, MixinCoords @@ -75,5 +77,26 @@ def setUp(self): self.draw_func = iplt.contourf +@tests.skip_plot +class TestAntialias(tests.IrisTest): + def test_skip_contour(self): + # Contours should not be added if data is all below second level. See #4086. + cube = simple_2d() + + levels = [5, 15, 20, 200] + colors = ["b", "r", "y"] + + iplt.contourf(cube, levels=levels, colors=colors, antialiased=True) + + ax = plt.gca() + # Expect 3 PathCollection objects (one for each colour) and no LineCollection + # objects. + for collection in ax.collections: + self.assertIsInstance( + collection, matplotlib.collections.PathCollection + ) + self.assertEqual(len(ax.collections), 3) + + if __name__ == "__main__": tests.main()