Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 851f769

Browse files
authored
Merge pull request #31280 from meeseeksmachine/auto-backport-of-pr-31278-on-v3.10.x
Backport PR #31278 on branch v3.10.x (Fix `clabel` manual argument not accepting unit-typed coordinates)
2 parents 52059e4 + ef19bdc commit 851f769

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

lib/matplotlib/contour.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
446446
if transform is None:
447447
transform = self.axes.transData
448448
if transform:
449+
x = self.axes.convert_xunits(x)
450+
y = self.axes.convert_yunits(y)
449451
x, y = transform.transform((x, y))
450452

451453
idx_level_min, idx_vtx_min, proj = self._find_nearest_contour(

lib/matplotlib/tests/test_datetime.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,22 @@ def test_bxp(self):
259259
ax.xaxis.set_major_formatter(mpl.dates.DateFormatter("%Y-%m-%d"))
260260
ax.set_title('Box plot with datetime data')
261261

262-
@pytest.mark.xfail(reason="Test for clabel not written yet")
263262
@mpl.style.context("default")
264263
def test_clabel(self):
264+
dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i)
265+
for i in range(10)]
266+
x = np.arange(-10.0, 5.0, 0.5)
267+
X, Y = np.meshgrid(x, dates)
268+
Z = np.arange(X.size).reshape(X.shape)
269+
265270
fig, ax = plt.subplots()
266-
ax.clabel(...)
271+
CS = ax.contour(X, Y, Z)
272+
labels = ax.clabel(CS, manual=[(x[0], dates[0])])
273+
assert len(labels) == 1
274+
assert labels[0].get_text() == '0'
275+
x_pos, y_pos = labels[0].get_position()
276+
assert x_pos == pytest.approx(-10.0, abs=1e-3)
277+
assert y_pos == pytest.approx(mpl.dates.date2num(dates[0]), abs=1e-3)
267278

268279
@mpl.style.context("default")
269280
def test_contour(self):

0 commit comments

Comments
 (0)