Skip to content

[Bug]: error using add_collection3d of Line3DCollection with autolims=True and lines containing different numbers of points #30418

@lxvm

Description

@lxvm

Bug summary

The docs state here that Line3DCollection should work with lines containing different numbers of points. However, calling ax.add_collection3d using a Line3DCollection of lines of different lengths with default keywords errors.

Code for reproduction

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import art3d

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
edges =  [
    [[0.25, -0.25, 0.0], [0.0, -0.25, 0.0], [0.25, -0.25, 0.25], [0.25, -0.25, 0.0]],
    [[0.25, 0.0, 0.0], [0.25, 0.25, 0.25], [0.25, -0.25, 0.25], [0.25, -0.25, 0.0], [0.25, 0.0, 0.0]],
    [[0.25, 0.0, 0.0], [0.0, 0.0, 0.0], [0.25, 0.25, 0.25], [0.25, 0.0, 0.0]],
    [[0.25, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, -0.25, 0.0], [0.25, -0.25, 0.0], [0.25, 0.0, 0.0]],
    [[0.0, 0.0, 0.0], [0.25, 0.25, 0.25], [0.25, -0.25, 0.25], [0.0, -0.25, 0.0], [0.0, 0.0, 0.0]],
]
l=art3d.Line3DCollection(edges, colors="black", linewidths=1)

ax.add_collection3d(l, autolim=True)

Actual outcome

Traceback (most recent call last):
  File "/local/home/lxvm/projects/autobz/mpl_mwe.py", line 16, in <module>
    ax.add_collection3d(l, autolim=True)
  File "/home/lxvm/.venv/mpl/lib/python3.11/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 2875, in add_collection3d
    self.auto_scale_xyz(*np.array(col._segments3d).transpose(),
                         ^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (5,) + inhomogeneous part.

Expected outcome

No error expected

Additional information

I found that autolims=False avoids the error and that the bug is introduced in 7ee56ce.
The issue is that in this line

self.auto_scale_xyz(*np.array(col._segments3d).transpose(),
the casting of the input lines as a Numpy array doesn't work for lines with different lengths (ragged arrays), which should be supported according to the docs.
This broke the plotting code in the first example of the readme of the following package: https://github.com/jerjorg/SymmetryReduceBZ.jl

Operating system

Debian

Matplotlib Version

3.10.5

Matplotlib Backend

agg

Python version

3.11.2

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions