Skip to content

Commit 8c7a193

Browse files
authored
Merge pull request matplotlib#31347 from timhoffm/deprecate-contourf-clabel
FIX: Deprecate using clabel() with filled contours
2 parents 6fc87ef + 169a730 commit 8c7a193

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Contour labelling on filled contours
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Using `~.Axes.clabel` to label filled contours created with `~.Axes.contourf` is deprecated. ``clabel()``
5+
is designed to label contour lines (`.Axes.contour`), and using it with filled contours can lead to inconsistent
6+
plots. If you want to add labels to filled contours, the recommended approach is to first create the filled contours
7+
with `~.Axes.contourf`, then overlay contour lines using `~.Axes.contour`, and finally apply `~.Axes.clabel` to those
8+
contour lines for labeling. For an example see :doc:`/gallery/images_contours_and_fields/contourf_demo`.

lib/matplotlib/contour.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ def clabel(self, levels=None, *,
145145
A list of `.Text` instances for the labels.
146146
"""
147147

148+
if self.filled:
149+
_api.warn_deprecated(
150+
"3.11",
151+
message="clabel() is not designed to be used with filled contours and "
152+
"may result in inconsistent plots. Applying clabel() to filled "
153+
"contours is thus deprecated since %(since)s. If you need "
154+
"labels with filled contours, instead draw contour lines "
155+
"using contour() in addition to contourf() and add the labels "
156+
"to the contour lines."
157+
)
158+
148159
# Based on the input arguments, clabel() adds a list of "label
149160
# specific" attributes to the ContourSet object. These attributes are
150161
# all of the form label* and names should be fairly self explanatory.

lib/matplotlib/tests/test_contour.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,15 @@ def test_label_contour_start():
310310
assert 0 in idxs
311311

312312

313+
def test_clabel_raises_on_filled_contours():
314+
X, Y = np.meshgrid(np.arange(10), np.arange(10))
315+
_, ax = plt.subplots()
316+
cs = ax.contourf(X, Y, X + Y)
317+
# will be an exception once the deprecation expires
318+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
319+
ax.clabel(cs)
320+
321+
313322
@image_comparison(['contour_corner_mask_False.png', 'contour_corner_mask_True.png'],
314323
remove_text=True, tol=1.88)
315324
def test_corner_mask():
@@ -359,21 +368,16 @@ def test_clabel_zorder(use_clabeltext, contour_zorder, clabel_zorder):
359368
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))
360369
z = np.max(np.dstack([abs(x), abs(y)]), 2)
361370

362-
fig, (ax1, ax2) = plt.subplots(ncols=2)
363-
cs = ax1.contour(x, y, z, zorder=contour_zorder)
364-
cs_filled = ax2.contourf(x, y, z, zorder=contour_zorder)
365-
clabels1 = cs.clabel(zorder=clabel_zorder, use_clabeltext=use_clabeltext)
366-
clabels2 = cs_filled.clabel(zorder=clabel_zorder,
367-
use_clabeltext=use_clabeltext)
371+
fig, ax = plt.subplots()
372+
cs = ax.contour(x, y, z, zorder=contour_zorder)
373+
clabels = cs.clabel(zorder=clabel_zorder, use_clabeltext=use_clabeltext)
368374

369375
if clabel_zorder is None:
370376
expected_clabel_zorder = 2+contour_zorder
371377
else:
372378
expected_clabel_zorder = clabel_zorder
373379

374-
for clabel in clabels1:
375-
assert clabel.get_zorder() == expected_clabel_zorder
376-
for clabel in clabels2:
380+
for clabel in clabels:
377381
assert clabel.get_zorder() == expected_clabel_zorder
378382

379383

0 commit comments

Comments
 (0)