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

Skip to content

Commit 95743f0

Browse files
authored
Merge pull request #27338 from meeseeksmachine/auto-backport-of-pr-27334-on-v3.8.x
Backport PR #27334 on branch v3.8.x (Omit MOVETO lines from nearest contour logic)
2 parents 38a0e94 + 26fd30a commit 95743f0

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

lib/matplotlib/contour.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,15 +1392,18 @@ def _find_nearest_contour(self, xy, indices=None):
13921392

13931393
for idx_level in indices:
13941394
path = self._paths[idx_level]
1395-
if not len(path.vertices):
1396-
continue
1397-
lc = self.get_transform().transform(path.vertices)
1398-
d2, proj, leg = _find_closest_point_on_path(lc, xy)
1399-
if d2 < d2min:
1400-
d2min = d2
1401-
idx_level_min = idx_level
1402-
idx_vtx_min = leg[1]
1403-
proj_min = proj
1395+
idx_vtx_start = 0
1396+
for subpath in path._iter_connected_components():
1397+
if not len(subpath.vertices):
1398+
continue
1399+
lc = self.get_transform().transform(subpath.vertices)
1400+
d2, proj, leg = _find_closest_point_on_path(lc, xy)
1401+
if d2 < d2min:
1402+
d2min = d2
1403+
idx_level_min = idx_level
1404+
idx_vtx_min = leg[1] + idx_vtx_start
1405+
proj_min = proj
1406+
idx_vtx_start += len(subpath)
14041407

14051408
return idx_level_min, idx_vtx_min, proj_min
14061409

lib/matplotlib/tests/test_contour.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ def test_contour_manual_labels(split_collections):
125125
plt.clabel(cs, manual=pts, fontsize='small', colors=('r', 'g'))
126126

127127

128+
def test_contour_manual_moveto():
129+
x = np.linspace(-10, 10)
130+
y = np.linspace(-10, 10)
131+
132+
X, Y = np.meshgrid(x, y)
133+
134+
Z = X**2 * 1 / Y**2 - 1
135+
136+
contours = plt.contour(X, Y, Z, levels=[0, 100])
137+
138+
# This point lies on the `MOVETO` line for the 100 contour
139+
# but is actually closest to the 0 contour
140+
point = (1.3, 1)
141+
clabels = plt.clabel(contours, manual=[point])
142+
143+
# Ensure that the 0 contour was chosen, not the 100 contour
144+
assert clabels[0].get_text() == "0"
145+
146+
128147
@pytest.mark.parametrize("split_collections", [False, True])
129148
@image_comparison(['contour_disconnected_segments'],
130149
remove_text=True, style='mpl20', extensions=['png'])

0 commit comments

Comments
 (0)