@@ -470,6 +470,48 @@ def test_contour_line_start_on_corner_edge():
470
470
cbar .add_lines (lines )
471
471
472
472
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
+
473
515
@mpl .style .context ("default" )
474
516
def test_contour_autolabel_beyond_powerlimits ():
475
517
ax = plt .figure ().add_subplot ()
0 commit comments