Skip to content

Commit 3b2af05

Browse files
committed
FIX: Deprecate using clabel() with filled contours
It's never been intended to be used that way and may cause inconsistent plots. Closes matplotlib#31344.
1 parent 0f46f27 commit 3b2af05

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

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: 9 additions & 0 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():

0 commit comments

Comments
 (0)