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

Skip to content

Fixed bug in find_nearest_contour #22767

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,8 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
"""
Find the point in the contour plot that is closest to ``(x, y)``.

This method does not support filled contours.

Parameters
----------
x, y : float
Expand Down Expand Up @@ -1370,8 +1372,11 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
# sufficiently well that the time is not noticeable.
# Nonetheless, improvements could probably be made.

if self.filled:
raise ValueError("Method does not support filled contours.")

if indices is None:
indices = range(len(self.levels))
indices = range(len(self.collections))

d2min = np.inf
conmin = None
Expand Down
42 changes: 42 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,48 @@ def test_contour_line_start_on_corner_edge():
cbar.add_lines(lines)


def test_find_nearest_contour():
xy = np.indices((15, 15))
img = np.exp(-np.pi * (np.sum((xy - 5)**2, 0)/5.**2))
cs = plt.contour(img, 10)

nearest_contour = cs.find_nearest_contour(1, 1, pixel=False)
expected_nearest = (1, 0, 33, 1.965966, 1.965966, 1.866183)
assert_array_almost_equal(nearest_contour, expected_nearest)

nearest_contour = cs.find_nearest_contour(8, 1, pixel=False)
expected_nearest = (1, 0, 5, 7.550173, 1.587542, 0.547550)
assert_array_almost_equal(nearest_contour, expected_nearest)

nearest_contour = cs.find_nearest_contour(2, 5, pixel=False)
expected_nearest = (3, 0, 21, 1.884384, 5.023335, 0.013911)
assert_array_almost_equal(nearest_contour, expected_nearest)

nearest_contour = cs.find_nearest_contour(2, 5,
indices=(5, 7),
pixel=False)
expected_nearest = (5, 0, 16, 2.628202, 5.0, 0.394638)
assert_array_almost_equal(nearest_contour, expected_nearest)


def test_find_nearest_contour_no_filled():
xy = np.indices((15, 15))
img = np.exp(-np.pi * (np.sum((xy - 5)**2, 0)/5.**2))
cs = plt.contourf(img, 10)

with pytest.raises(ValueError,
match="Method does not support filled contours."):
cs.find_nearest_contour(1, 1, pixel=False)

with pytest.raises(ValueError,
match="Method does not support filled contours."):
cs.find_nearest_contour(1, 10, indices=(5, 7), pixel=False)

with pytest.raises(ValueError,
match="Method does not support filled contours."):
cs.find_nearest_contour(2, 5, indices=(2, 7), pixel=True)


@mpl.style.context("default")
def test_contour_autolabel_beyond_powerlimits():
ax = plt.figure().add_subplot()
Expand Down