Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4a47543

Browse filesBrowse files
Fixed bug in find_nearest_contour; Added test
1 parent 6c3412b commit 4a47543
Copy full SHA for 4a47543

File tree

Expand file treeCollapse file tree

2 files changed

+48
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+48
-1
lines changed

‎lib/matplotlib/contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/contour.py
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,8 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
13341334
"""
13351335
Find the point in the contour plot that is closest to ``(x, y)``.
13361336
1337+
This method does not support filled contours.
1338+
13371339
Parameters
13381340
----------
13391341
x, y : float
@@ -1370,8 +1372,11 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
13701372
# sufficiently well that the time is not noticeable.
13711373
# Nonetheless, improvements could probably be made.
13721374

1375+
if self.filled:
1376+
raise ValueError("Method does not support filled contours.")
1377+
13731378
if indices is None:
1374-
indices = range(len(self.levels))
1379+
indices = range(len(self.collections))
13751380

13761381
d2min = np.inf
13771382
conmin = None

‎lib/matplotlib/tests/test_contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_contour.py
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,48 @@ def test_contour_line_start_on_corner_edge():
470470
cbar.add_lines(lines)
471471

472472

473+
def test_find_nearest_contour():
474+
xy = np.indices((15, 15))
475+
img = np.exp(-np.pi * (np.sum((xy - 5)**2, 0)/5.**2))
476+
cs = plt.contour(img, 10)
477+
478+
nearest_contour = cs.find_nearest_contour(1, 1, pixel=False)
479+
expected_nearest = (1, 0, 33, 1.965966, 1.965966, 1.866183)
480+
assert_array_almost_equal(nearest_contour, expected_nearest)
481+
482+
nearest_contour = cs.find_nearest_contour(8, 1, pixel=False)
483+
expected_nearest = (1, 0, 5, 7.550173, 1.587542, 0.547550)
484+
assert_array_almost_equal(nearest_contour, expected_nearest)
485+
486+
nearest_contour = cs.find_nearest_contour(2, 5, pixel=False)
487+
expected_nearest = (3, 0, 21, 1.884384, 5.023335, 0.013911)
488+
assert_array_almost_equal(nearest_contour, expected_nearest)
489+
490+
nearest_contour = cs.find_nearest_contour(2, 5,
491+
indices=(5, 7),
492+
pixel=False)
493+
expected_nearest = (5, 0, 16, 2.628202, 5.0, 0.394638)
494+
assert_array_almost_equal(nearest_contour, expected_nearest)
495+
496+
497+
def test_find_nearest_contour_no_filled():
498+
xy = np.indices((15, 15))
499+
img = np.exp(-np.pi * (np.sum((xy - 5)**2, 0)/5.**2))
500+
cs = plt.contourf(img, 10)
501+
502+
with pytest.raises(ValueError,
503+
match="Method does not support filled contours."):
504+
cs.find_nearest_contour(1, 1, pixel=False)
505+
506+
with pytest.raises(ValueError,
507+
match="Method does not support filled contours."):
508+
cs.find_nearest_contour(1, 10, indices=(5, 7), pixel=False)
509+
510+
with pytest.raises(ValueError,
511+
match="Method does not support filled contours."):
512+
cs.find_nearest_contour(2, 5, indices=(2, 7), pixel=True)
513+
514+
473515
@mpl.style.context("default")
474516
def test_contour_autolabel_beyond_powerlimits():
475517
ax = plt.figure().add_subplot()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.