Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit MOVETO lines from nearest contour logic #27334

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,15 +1339,18 @@ def _find_nearest_contour(self, xy, indices=None):

for idx_level in indices:
path = self._paths[idx_level]
if not len(path.vertices):
continue
lc = self.get_transform().transform(path.vertices)
d2, proj, leg = _find_closest_point_on_path(lc, xy)
if d2 < d2min:
d2min = d2
idx_level_min = idx_level
idx_vtx_min = leg[1]
proj_min = proj
idx_vtx_start = 0
for subpath in path._iter_connected_components():
if not len(subpath.vertices):
continue
lc = self.get_transform().transform(subpath.vertices)
d2, proj, leg = _find_closest_point_on_path(lc, xy)
if d2 < d2min:
d2min = d2
idx_level_min = idx_level
idx_vtx_min = leg[1] + idx_vtx_start
proj_min = proj
idx_vtx_start += len(subpath)

return idx_level_min, idx_vtx_min, proj_min

Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ def test_contour_manual_labels(split_collections):
plt.clabel(cs, manual=pts, fontsize='small', colors=('r', 'g'))


def test_contour_manual_moveto():
x = np.linspace(-10, 10)
y = np.linspace(-10, 10)

X, Y = np.meshgrid(x, y)

Z = X**2 * 1 / Y**2 - 1

contours = plt.contour(X, Y, Z, levels=[0, 100])

# This point lies on the `MOVETO` line for the 100 contour
# but is actually closest to the 0 contour
point = (1.3, 1)
clabels = plt.clabel(contours, manual=[point])

# Ensure that the 0 contour was chosen, not the 100 contour
assert clabels[0].get_text() == "0"


@pytest.mark.parametrize("split_collections", [False, True])
@image_comparison(['contour_disconnected_segments'],
remove_text=True, style='mpl20', extensions=['png'])
Expand Down