Skip to content

Commit ce62b25

Browse files
Fix IndexError in ax.clabel manual positioning (#31702)
1 parent 5ffcca9 commit ce62b25

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

lib/matplotlib/contour.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
478478
label_width = self._get_nth_label_width(level)
479479
rotation, path = self._split_path_and_get_label_rotation(
480480
path, idx_vtx_min, proj, label_width, inline_spacing)
481-
self.add_label(*proj, rotation, self.labelLevelList[idx_level_min],
482-
self.labelCValueList[idx_level_min])
481+
self.add_label(*proj, rotation, self.labelLevelList[level],
482+
self.labelCValueList[level])
483483

484484
if inline:
485485
self._paths[idx_level_min] = path

lib/matplotlib/tests/test_contour.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,12 @@ def test_contour_aliases(fig_test, fig_ref):
891891
def test_contour_singular_color():
892892
with pytest.raises(TypeError):
893893
plt.figure().add_subplot().contour([[0, 1], [2, 3]], color="r")
894+
895+
896+
def test_clabel_manual_subset():
897+
# Test for issue #31702: clabel manual indexing with subset of levels
898+
import matplotlib.pyplot as plt
899+
fig, ax = plt.subplots()
900+
cs = ax.contour([[1, 2], [3, 4]], levels=[1.5, 2.5, 3.5])
901+
# Attempt to label only one specific level manually
902+
ax.clabel(cs, levels=[2.5], manual=[(0.5, 0.5)])

0 commit comments

Comments
 (0)