test__plotutils.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import pytest
  2. import numpy as np
  3. from numpy.testing import assert_, assert_array_equal, assert_allclose
  4. try:
  5. import matplotlib
  6. matplotlib.rcParams['backend'] = 'Agg'
  7. import matplotlib.pyplot as plt
  8. has_matplotlib = True
  9. except Exception:
  10. has_matplotlib = False
  11. from scipy.spatial import \
  12. delaunay_plot_2d, voronoi_plot_2d, convex_hull_plot_2d, \
  13. Delaunay, Voronoi, ConvexHull
  14. @pytest.mark.skipif(not has_matplotlib, reason="Matplotlib not available")
  15. class TestPlotting:
  16. points = [(0,0), (0,1), (1,0), (1,1)]
  17. def test_delaunay(self):
  18. # Smoke test
  19. fig = plt.figure()
  20. obj = Delaunay(self.points)
  21. s_before = obj.simplices.copy()
  22. r = delaunay_plot_2d(obj, ax=fig.gca())
  23. assert_array_equal(obj.simplices, s_before) # shouldn't modify
  24. assert_(r is fig)
  25. delaunay_plot_2d(obj, ax=fig.gca())
  26. def test_voronoi(self):
  27. # Smoke test
  28. fig = plt.figure()
  29. obj = Voronoi(self.points)
  30. r = voronoi_plot_2d(obj, ax=fig.gca())
  31. assert_(r is fig)
  32. voronoi_plot_2d(obj)
  33. voronoi_plot_2d(obj, show_vertices=False)
  34. def test_convex_hull(self):
  35. # Smoke test
  36. fig = plt.figure()
  37. tri = ConvexHull(self.points)
  38. r = convex_hull_plot_2d(tri, ax=fig.gca())
  39. assert_(r is fig)
  40. convex_hull_plot_2d(tri)
  41. def test_gh_19653(self):
  42. # aspect ratio sensitivity of voronoi_plot_2d
  43. # infinite Voronoi edges
  44. points = np.array([[245.059986986012, 10.971011721360075],
  45. [320.49044143557785, 10.970258360366753],
  46. [239.79023081978914, 13.108487516946218],
  47. [263.38325791238833, 12.93241352743668],
  48. [219.53334398353175, 13.346107628161008]])
  49. vor = Voronoi(points)
  50. fig = voronoi_plot_2d(vor)
  51. ax = fig.gca()
  52. infinite_segments = ax.collections[1].get_segments()
  53. expected_segments = np.array([[[282.77256, -254.76904],
  54. [282.729714, -4544.744698]],
  55. [[282.77256014, -254.76904029],
  56. [430.08561382, 4032.67658742]],
  57. [[229.26733285, -20.39957514],
  58. [-168.17167404, -4291.92545966]],
  59. [[289.93433364, 5151.40412217],
  60. [330.40553385, 9441.18887532]]])
  61. assert_allclose(infinite_segments, expected_segments)
  62. def test_gh_19653_smaller_aspect(self):
  63. # reasonable behavior for less extreme aspect
  64. # ratio
  65. points = np.array([[24.059986986012, 10.971011721360075],
  66. [32.49044143557785, 10.970258360366753],
  67. [23.79023081978914, 13.108487516946218],
  68. [26.38325791238833, 12.93241352743668],
  69. [21.53334398353175, 13.346107628161008]])
  70. vor = Voronoi(points)
  71. fig = voronoi_plot_2d(vor)
  72. ax = fig.gca()
  73. infinite_segments = ax.collections[1].get_segments()
  74. expected_segments = np.array([[[28.274979, 8.335027],
  75. [28.270463, -42.19763338]],
  76. [[28.27497869, 8.33502697],
  77. [43.73223829, 56.44555501]],
  78. [[22.51805823, 11.8621754],
  79. [-12.09266506, -24.95694485]],
  80. [[29.53092448, 78.46952378],
  81. [33.82572726, 128.81934455]]])
  82. assert_allclose(infinite_segments, expected_segments)