Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Bug]: Poly3DCollection initialization cannot properly handle parameter verts when it is a list of nested tuples and shade is False #29156

Closed
Labels
Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!topic: mplot3d
Milestone
@williamlus

Description

@williamlus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!topic: mplot3d

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp