diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 76481280729a..26a25c00dc09 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -478,8 +478,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5, label_width = self._get_nth_label_width(level) rotation, path = self._split_path_and_get_label_rotation( path, idx_vtx_min, proj, label_width, inline_spacing) - self.add_label(*proj, rotation, self.labelLevelList[idx_level_min], - self.labelCValueList[idx_level_min]) + self.add_label(*proj, rotation, self.labelLevelList[level], + self.labelCValueList[level]) if inline: self._paths[idx_level_min] = path diff --git a/lib/matplotlib/tests/test_contour.py b/lib/matplotlib/tests/test_contour.py index 3c810b026fce..fac4412273ea 100644 --- a/lib/matplotlib/tests/test_contour.py +++ b/lib/matplotlib/tests/test_contour.py @@ -891,3 +891,12 @@ def test_contour_aliases(fig_test, fig_ref): def test_contour_singular_color(): with pytest.raises(TypeError): plt.figure().add_subplot().contour([[0, 1], [2, 3]], color="r") + + +def test_clabel_manual_subset(): + # Test for issue #31702: clabel manual indexing with subset of levels + import matplotlib.pyplot as plt + fig, ax = plt.subplots() + cs = ax.contour([[1, 2], [3, 4]], levels=[1.5, 2.5, 3.5]) + # Attempt to label only one specific level manually + ax.clabel(cs, levels=[2.5], manual=[(0.5, 0.5)])