Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit4a47543

Browse files
Fixed bug in find_nearest_contour; Added test
1 parent6c3412b commit4a47543

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

‎lib/matplotlib/contour.py

Lines 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+
ifself.filled:
1376+
raiseValueError("Method does not support filled contours.")
1377+
13731378
ifindicesisNone:
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

Lines 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+
deftest_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+
deftest_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+
withpytest.raises(ValueError,
503+
match="Method does not support filled contours."):
504+
cs.find_nearest_contour(1,1,pixel=False)
505+
506+
withpytest.raises(ValueError,
507+
match="Method does not support filled contours."):
508+
cs.find_nearest_contour(1,10,indices=(5,7),pixel=False)
509+
510+
withpytest.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
deftest_contour_autolabel_beyond_powerlimits():
475517
ax=plt.figure().add_subplot()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp