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

Skip to content

Commit a01f57d

Browse files
authored
Fix clabel manual argument not accepting unit-typed coordinates (#31278)
* Fix clabel manual argument not accepting units * add test for clabel with datetime units * simplify test_clabel * Add assertions to test_clabel
1 parent 71e470d commit a01f57d

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
@@ -451,6 +451,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
451451
if transform is None:
452452
transform = self.axes.transData
453453
if transform:
454+
x = self.axes.convert_xunits(x)
455+
y = self.axes.convert_yunits(y)
454456
x, y = transform.transform((x, y))
455457

456458
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)