Skip to content

Commit

Permalink
Avoid using deprecated ContourSet attributes (#5405)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer committed Jul 31, 2023
1 parent 1efa4bf commit ea2a272
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
3 changes: 3 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ This document explains the changes made to Iris for this release
`"Xarray bridge" <https://github.com/SciTools/iris/issues/4994>`_ facility.
(:pull:`5214`)

#. `@rcomer`_ updated :func:`~iris.plot.contourf` to avoid using functionality
that is deprecated in Matplotlib v3.8 (:pull:`5405`)



.. comment
Expand Down
26 changes: 16 additions & 10 deletions lib/iris/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,16 +1155,26 @@ def contourf(cube, *args, **kwargs):
# But if the polygons are virtually opaque then we can cover the seams
# by drawing anti-aliased lines *underneath* the polygon joins.

# Figure out the alpha level for the contour plot
if result.alpha is None:
alpha = result.collections[0].get_facecolor()[0][3]
if hasattr(result, "get_antialiased"):
# Matplotlib v3.8 onwards.
antialiased = any(result.get_antialiased())
# Figure out the colours and alpha level for the contour plot
colors = result.get_facecolor()
alpha = result.alpha or colors[0][3]
# Define a zorder just *below* the polygons to ensure we minimise any boundary shift.
zorder = result.zorder - 0.1
else:
alpha = result.alpha
antialiased = result.antialiased
# Figure out the alpha level for the contour plot
alpha = result.alpha or result.collections[0].get_facecolor()[0][3]
colors = [c[0] for c in result.tcolors]
# Define a zorder just *below* the polygons to ensure we minimise any boundary shift.
zorder = result.collections[0].zorder - 0.1

# If the contours are anti-aliased and mostly opaque then draw lines under
# the seams.
if result.antialiased and alpha > 0.95:
if antialiased and alpha > 0.95:
levels = result.levels
colors = [c[0] for c in result.tcolors]
if result.extend == "neither":
levels = levels[1:-1]
colors = colors[:-1]
Expand All @@ -1177,11 +1187,7 @@ def contourf(cube, *args, **kwargs):
else:
colors = colors[:-1]
if len(levels) > 0 and np.nanmax(cube.data) > levels[0]:
# Draw the lines just *below* the polygons to ensure we minimise
# any boundary shift.
zorder = result.collections[0].zorder - 0.1
axes = kwargs.get("axes", None)

contour(
cube,
levels=levels,
Expand Down
6 changes: 2 additions & 4 deletions lib/iris/tests/unit/plot/test_contourf.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ def setUp(self):
self.bar_index = np.arange(self.bar.size)
self.data = self.cube.data
self.dataT = self.data.T
mocker = mock.Mock(alpha=0, antialiased=False)
self.mpl_patch = self.patch(
"matplotlib.pyplot.contourf", return_value=mocker
)
mocker = mock.Mock(wraps=plt.contourf)
self.mpl_patch = self.patch("matplotlib.pyplot.contourf", mocker)
self.draw_func = iplt.contourf


Expand Down
7 changes: 3 additions & 4 deletions lib/iris/tests/unit/quickplot/test_contourf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from unittest import mock

import matplotlib.pyplot as plt
import numpy as np

from iris.tests.stock import simple_2d
Expand Down Expand Up @@ -42,10 +43,8 @@ def setUp(self):
self.bar_index = np.arange(self.bar.size)
self.data = self.cube.data
self.dataT = self.data.T
mocker = mock.Mock(alpha=0, antialiased=False)
self.mpl_patch = self.patch(
"matplotlib.pyplot.contourf", return_value=mocker
)
mocker = mock.Mock(wraps=plt.contourf)
self.mpl_patch = self.patch("matplotlib.pyplot.contourf", mocker)
# Also need to mock the colorbar.
self.patch("matplotlib.pyplot.colorbar")
self.draw_func = qplt.contourf
Expand Down

0 comments on commit ea2a272

Please sign in to comment.