Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
The initialization of anmpl_toolkits.mplot3d.Poly3DCollection
object cannot properly handle the parameterverts
whenverts
= a list of (N, 3) array-like nested tuples, andshade=False
.
Code for reproduction
frommpl_toolkits.mplot3dimportart3dcorners= ((0,0,0), (0,5,0), (5,5,0), (5,0,0))tri=art3d.Poly3DCollection([corners],shade=True)# Failed when shade=True# tri = art3d.Poly3DCollection([corners]) # Passed with the default setting shade=False
Actual outcome
TypeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 tri = art3d.Poly3DCollection([corners], shade=True)
File ~/anaconda3/envs/testmpl/lib/python3.12/site-packages/mpl_toolkits/mplot3d/art3d.py:905, in Poly3DCollection.init(self, verts, zsort, shade, lightsource, *args, **kwargs)
875 """
876 Parameters
877 ----------
(...)
902 and _edgecolors properties.
903 """
904 if shade:
--> 905 normals = _generate_normals(verts)
906 facecolors = kwargs.get('facecolors', None)
907 if facecolors is not None:
File ~/anaconda3/envs/testmpl/lib/python3.12/site-packages/mpl_toolkits/mplot3d/art3d.py:1222, in _generate_normals(polygons)
1220 n = len(ps)
1221 i1, i2, i3 = 0, n//3, 2*n//3
-> 1222 v1[poly_i, :] = ps[i1, :] - ps[i2, :]
1223 v2[poly_i, :] = ps[i2, :] - ps[i3, :]
1224 return np.cross(v1, v2)
TypeError: tuple indices must be integers or slices, not tuple
Expected outcome
No error.
Additional information
Whenshade=True
, the__init__
function will first call the function_generate_normals(polygons)
, wherepolygons=verts
. In our case,verts
is not an instance ofnp.ndarray
but a list, so it enters the for loop:
forpoly_i,psinenumerate(polygons):n=len(ps)i1,i2,i3=0,n//3,2*n//3v1[poly_i, :]=ps[i1, :]-ps[i2, :]v2[poly_i, :]=ps[i2, :]-ps[i3, :]
polygons
is[((0, 0, 0), (0, 5, 0), (5, 5, 0), (5, 0, 0))]
, andps
is a nested tuple, so we need to convertps
tonp.ndarray
before using the array slicing likeps[i1, :]
. A possible fix may be settingps = np.asarray(ps)
before array slicing.
Operating system
No response
Matplotlib Version
3.9.2
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
None