From 6332e6c1b400456edcda8b0890c15bd394520424 Mon Sep 17 00:00:00 2001 From: Aman Srivastava Date: Wed, 11 Mar 2026 13:27:40 +0530 Subject: [PATCH 1/4] Fix clabel manual argument not accepting units --- lib/matplotlib/contour.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 77dc58740971..287794e00ad5 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -451,6 +451,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5, if transform is None: transform = self.axes.transData if transform: + x = self.axes.convert_xunits(x) + y = self.axes.convert_yunits(y) x, y = transform.transform((x, y)) idx_level_min, idx_vtx_min, proj = self._find_nearest_contour( From 3a77348fb5cb922e2a39340b3cc44c85f68e4ded Mon Sep 17 00:00:00 2001 From: Aman Srivastava Date: Wed, 11 Mar 2026 16:07:32 +0530 Subject: [PATCH 2/4] add test for clabel with datetime units --- lib/matplotlib/tests/test_datetime.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index b3bd4d7bd151..fb6cf359e2e8 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -259,11 +259,21 @@ def test_bxp(self): ax.xaxis.set_major_formatter(mpl.dates.DateFormatter("%Y-%m-%d")) ax.set_title('Box plot with datetime data') - @pytest.mark.xfail(reason="Test for clabel not written yet") @mpl.style.context("default") def test_clabel(self): + mpl.rcParams["date.converter"] = "concise" + dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) + for i in range(10)] + x = np.arange(-10.0, 5.0, 0.5) + X, Y = np.meshgrid(x, dates) + Z = np.arange(X.size).reshape(X.shape) + fig, ax = plt.subplots() - ax.clabel(...) + CS = ax.contour(X, Y, Z) + labels = ax.clabel(CS, CS.levels, inline=True, + fmt=dict(zip(CS.levels, dates)), + manual=[(xi, yi) for xi, yi in zip(x, dates)]) + assert len(labels) > 0 @mpl.style.context("default") def test_contour(self): From c088b57e4df5e3dd75eaded1859699180977b484 Mon Sep 17 00:00:00 2001 From: Aman Srivastava Date: Wed, 11 Mar 2026 16:33:33 +0530 Subject: [PATCH 3/4] simplify test_clabel --- lib/matplotlib/tests/test_datetime.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index fb6cf359e2e8..c82b71f238ae 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -261,7 +261,6 @@ def test_bxp(self): @mpl.style.context("default") def test_clabel(self): - mpl.rcParams["date.converter"] = "concise" dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) for i in range(10)] x = np.arange(-10.0, 5.0, 0.5) @@ -270,10 +269,7 @@ def test_clabel(self): fig, ax = plt.subplots() CS = ax.contour(X, Y, Z) - labels = ax.clabel(CS, CS.levels, inline=True, - fmt=dict(zip(CS.levels, dates)), - manual=[(xi, yi) for xi, yi in zip(x, dates)]) - assert len(labels) > 0 + ax.clabel(CS, manual=[(x[0], dates[0])]) @mpl.style.context("default") def test_contour(self): From fd27483f8e055f508430b9dd99b1107f84b2e888 Mon Sep 17 00:00:00 2001 From: Aman Srivastava Date: Wed, 11 Mar 2026 22:35:23 +0530 Subject: [PATCH 4/4] Add assertions to test_clabel --- lib/matplotlib/tests/test_datetime.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_datetime.py b/lib/matplotlib/tests/test_datetime.py index c82b71f238ae..c246fb23f011 100644 --- a/lib/matplotlib/tests/test_datetime.py +++ b/lib/matplotlib/tests/test_datetime.py @@ -262,14 +262,19 @@ def test_bxp(self): @mpl.style.context("default") def test_clabel(self): dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) - for i in range(10)] + for i in range(10)] x = np.arange(-10.0, 5.0, 0.5) X, Y = np.meshgrid(x, dates) Z = np.arange(X.size).reshape(X.shape) fig, ax = plt.subplots() CS = ax.contour(X, Y, Z) - ax.clabel(CS, manual=[(x[0], dates[0])]) + labels = ax.clabel(CS, manual=[(x[0], dates[0])]) + assert len(labels) == 1 + assert labels[0].get_text() == '0' + x_pos, y_pos = labels[0].get_position() + assert x_pos == pytest.approx(-10.0, abs=1e-3) + assert y_pos == pytest.approx(mpl.dates.date2num(dates[0]), abs=1e-3) @mpl.style.context("default") def test_contour(self):