Skip to content

Commit

Permalink
Contourf Antialias Fix (#4150)
Browse files Browse the repository at this point in the history
* check data max

* add test

* whatsnew

* fix PR number
  • Loading branch information
rcomer authored May 25, 2021
1 parent c82dc37 commit aee4ce7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/iris/tests/unit/plot/test_contourf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit aee4ce7

Please sign in to comment.