test_art3d.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.backend_bases import MouseEvent
  4. from mpl_toolkits.mplot3d.art3d import (
  5. Line3DCollection,
  6. Poly3DCollection,
  7. _all_points_on_plane,
  8. )
  9. def test_scatter_3d_projection_conservation():
  10. fig = plt.figure()
  11. ax = fig.add_subplot(projection='3d')
  12. # fix axes3d projection
  13. ax.roll = 0
  14. ax.elev = 0
  15. ax.azim = -45
  16. ax.stale = True
  17. x = [0, 1, 2, 3, 4]
  18. scatter_collection = ax.scatter(x, x, x)
  19. fig.canvas.draw_idle()
  20. # Get scatter location on canvas and freeze the data
  21. scatter_offset = scatter_collection.get_offsets()
  22. scatter_location = ax.transData.transform(scatter_offset)
  23. # Yaw -44 and -46 are enough to produce two set of scatter
  24. # with opposite z-order without moving points too far
  25. for azim in (-44, -46):
  26. ax.azim = azim
  27. ax.stale = True
  28. fig.canvas.draw_idle()
  29. for i in range(5):
  30. # Create a mouse event used to locate and to get index
  31. # from each dots
  32. event = MouseEvent("button_press_event", fig.canvas,
  33. *scatter_location[i, :])
  34. contains, ind = scatter_collection.contains(event)
  35. assert contains is True
  36. assert len(ind["ind"]) == 1
  37. assert ind["ind"][0] == i
  38. def test_zordered_error():
  39. # Smoke test for https://github.com/matplotlib/matplotlib/issues/26497
  40. lc = [(np.fromiter([0.0, 0.0, 0.0], dtype="float"),
  41. np.fromiter([1.0, 1.0, 1.0], dtype="float"))]
  42. pc = [np.fromiter([0.0, 0.0], dtype="float"),
  43. np.fromiter([0.0, 1.0], dtype="float"),
  44. np.fromiter([1.0, 1.0], dtype="float")]
  45. fig = plt.figure()
  46. ax = fig.add_subplot(projection="3d")
  47. ax.add_collection(Line3DCollection(lc))
  48. ax.scatter(*pc, visible=False)
  49. plt.draw()
  50. def test_all_points_on_plane():
  51. # Non-coplanar points
  52. points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
  53. assert not _all_points_on_plane(*points.T)
  54. # Duplicate points
  55. points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0]])
  56. assert _all_points_on_plane(*points.T)
  57. # NaN values
  58. points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, np.nan]])
  59. assert _all_points_on_plane(*points.T)
  60. # Less than 3 unique points
  61. points = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
  62. assert _all_points_on_plane(*points.T)
  63. # All points lie on a line
  64. points = np.array([[0, 0, 0], [0, 1, 0], [0, 2, 0], [0, 3, 0]])
  65. assert _all_points_on_plane(*points.T)
  66. # All points lie on two lines, with antiparallel vectors
  67. points = np.array([[-2, 2, 0], [-1, 1, 0], [1, -1, 0],
  68. [0, 0, 0], [2, 0, 0], [1, 0, 0]])
  69. assert _all_points_on_plane(*points.T)
  70. # All points lie on a plane
  71. points = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0], [1, 2, 0]])
  72. assert _all_points_on_plane(*points.T)
  73. def test_generate_normals():
  74. # Smoke test for https://github.com/matplotlib/matplotlib/issues/29156
  75. vertices = ((0, 0, 0), (0, 5, 0), (5, 5, 0), (5, 0, 0))
  76. shape = Poly3DCollection([vertices], edgecolors='r', shade=True)
  77. fig = plt.figure()
  78. ax = fig.add_subplot(projection='3d')
  79. ax.add_collection3d(shape)
  80. plt.draw()