From 595fde3b7007f31ff0c38356f1310194b7a8acc2 Mon Sep 17 00:00:00 2001 From: Victor Liu Date: Fri, 22 Nov 2024 21:22:36 -0500 Subject: [PATCH 1/5] converted ps to array before slicing --- lib/mpl_toolkits/mplot3d/art3d.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index 0467d2e96e5e..deb0ca34302c 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -1382,6 +1382,7 @@ def _generate_normals(polygons): v2 = np.empty((len(polygons), 3)) for poly_i, ps in enumerate(polygons): n = len(ps) + ps = np.asarray(ps) i1, i2, i3 = 0, n//3, 2*n//3 v1[poly_i, :] = ps[i1, :] - ps[i2, :] v2[poly_i, :] = ps[i2, :] - ps[i3, :] From 7527850ae0aa19f1a1865c08369ec8580d5499b5 Mon Sep 17 00:00:00 2001 From: Victor Liu Date: Sat, 23 Nov 2024 14:30:11 -0500 Subject: [PATCH 2/5] added test cases for the modification to art3d.py --- lib/mpl_toolkits/mplot3d/tests/test_art3d.py | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py index f4f7067b76bb..7760b0593ab6 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py @@ -3,7 +3,7 @@ import matplotlib.pyplot as plt from matplotlib.backend_bases import MouseEvent -from mpl_toolkits.mplot3d.art3d import Line3DCollection, _all_points_on_plane +from mpl_toolkits.mplot3d.art3d import Line3DCollection, Poly3DCollection, _all_points_on_plane def test_scatter_3d_projection_conservation(): @@ -85,3 +85,32 @@ def test_all_points_on_plane(): # All points lie on a plane points = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0], [1, 2, 0]]) assert _all_points_on_plane(*points.T) + + +def test_generate_normals(): + + # Following code is an example taken from + # https://stackoverflow.com/questions/18897786/transparency-for-poly3dcollection-plot-in-matplotlib + # and modified to test _generate_normals function + + fig = plt.figure() + ax = fig.add_subplot(111, projection='3d') + + x = [0, 2, 1, 1] + y = [0, 0, 1, 0] + z = [0, 0, 0, 1] + + # deliberately use nested tuple + vertices = ((0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)) + + tupleList = list(zip(x, y, z)) + + poly3d = [[tupleList[vertices[ix][iy]] for iy in range(len(vertices[0]))] + for ix in range(len(vertices))] + ax.scatter(x, y, z) + collection = Poly3DCollection(poly3d, alpha=0.2, edgecolors='r', shade=True) + face_color = [0.5, 0.5, 1] # alternative: matplotlib.colors.rgb2hex([0.5, 0.5, 1]) + collection.set_facecolor(face_color) + ax.add_collection3d(collection) + + plt.draw() From 54b57b4a3dd1b4c74c50c64e554a28166f5f3601 Mon Sep 17 00:00:00 2001 From: Victor Liu Date: Sat, 7 Dec 2024 15:39:07 -0500 Subject: [PATCH 3/5] modified test for _generate_normals --- lib/mpl_toolkits/mplot3d/tests/test_art3d.py | 30 ++++---------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py index 7760b0593ab6..d51fb1bc83fd 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py @@ -88,29 +88,11 @@ def test_all_points_on_plane(): def test_generate_normals(): - - # Following code is an example taken from - # https://stackoverflow.com/questions/18897786/transparency-for-poly3dcollection-plot-in-matplotlib - # and modified to test _generate_normals function + # Smoke test for https://github.com/matplotlib/matplotlib/issues/29156 + vertices = ((0, 0, 0), (0, 5, 0), (5, 5, 0), (5, 0, 0)) + shape = Poly3DCollection([vertices], edgecolors='r', shade=True) fig = plt.figure() - ax = fig.add_subplot(111, projection='3d') - - x = [0, 2, 1, 1] - y = [0, 0, 1, 0] - z = [0, 0, 0, 1] - - # deliberately use nested tuple - vertices = ((0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)) - - tupleList = list(zip(x, y, z)) - - poly3d = [[tupleList[vertices[ix][iy]] for iy in range(len(vertices[0]))] - for ix in range(len(vertices))] - ax.scatter(x, y, z) - collection = Poly3DCollection(poly3d, alpha=0.2, edgecolors='r', shade=True) - face_color = [0.5, 0.5, 1] # alternative: matplotlib.colors.rgb2hex([0.5, 0.5, 1]) - collection.set_facecolor(face_color) - ax.add_collection3d(collection) - - plt.draw() + ax = fig.add_subplot(projection='3d') + ax.add_collection3d(shape) + plt.show() From c73088ae05ada9ad6aec534abb28fb327a6669e1 Mon Sep 17 00:00:00 2001 From: Victor Liu Date: Sat, 7 Dec 2024 16:04:59 -0500 Subject: [PATCH 4/5] changed plot.show to plot.draw --- lib/mpl_toolkits/mplot3d/tests/test_art3d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py index d51fb1bc83fd..cd4ed87951d8 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py @@ -95,4 +95,4 @@ def test_generate_normals(): fig = plt.figure() ax = fig.add_subplot(projection='3d') ax.add_collection3d(shape) - plt.show() + plt.draw() From e5f576569eb39a8ec27f3b06b2dca09e5cc5b52f Mon Sep 17 00:00:00 2001 From: Scott Shambaugh Date: Wed, 11 Dec 2024 12:58:11 -0700 Subject: [PATCH 5/5] linting --- lib/mpl_toolkits/mplot3d/tests/test_art3d.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py index cd4ed87951d8..174c12608ae9 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_art3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_art3d.py @@ -3,7 +3,11 @@ import matplotlib.pyplot as plt from matplotlib.backend_bases import MouseEvent -from mpl_toolkits.mplot3d.art3d import Line3DCollection, Poly3DCollection, _all_points_on_plane +from mpl_toolkits.mplot3d.art3d import ( + Line3DCollection, + Poly3DCollection, + _all_points_on_plane, +) def test_scatter_3d_projection_conservation():