Repository: matplotlib/matplotlib

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

The docs state [here](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.art3d.Line3DCollection.html) 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

```Python
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 https://github.com/matplotlib/matplotlib/commit/7ee56ce31f3c2719ee774132ed36f5b1ca549afa.
The issue is that in this line https://github.com/matplotlib/matplotlib/blob/0a27401ecdede3081f05903e7b9b4c8531248c39/lib/mpl_toolkits/mplot3d/axes3d.py#L2892 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


Discussion:

Patch:
diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py
index c56e4c6b7039..3122b64bde73 100644
--- a/lib/mpl_toolkits/mplot3d/axes3d.py
+++ b/lib/mpl_toolkits/mplot3d/axes3d.py
@@ -2890,8 +2890,10 @@ def add_collection3d(self, col, zs=0, zdir='z', autolim=True, *,
 
         if autolim:
             if isinstance(col, art3d.Line3DCollection):
-                self.auto_scale_xyz(*np.array(col._segments3d).transpose(),
-                                    had_data=had_data)
+                # Handle ragged arrays by extracting coordinates separately
+                all_points = np.concatenate(col._segments3d)
+                self.auto_scale_xyz(all_points[:, 0], all_points[:, 1],
+                                    all_points[:, 2], had_data=had_data)
             elif isinstance(col, art3d.Poly3DCollection):
                 self.auto_scale_xyz(col._faces[..., 0],
                                     col._faces[..., 1],


Test patch:
diff --git a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
index e6d11f793b46..370fd506530e 100644
--- a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
+++ b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
@@ -2689,3 +2689,25 @@ def test_ndarray_color_kwargs_value_error():
     ax = fig.add_subplot(111, projection='3d')
     ax.scatter(1, 0, 0, color=np.array([0, 0, 0, 1]))
     fig.canvas.draw()
+
+
+def test_line3dcollection_autolim_ragged():
+    """Test Line3DCollection with autolim=True and lines of different lengths."""
+    fig = plt.figure()
+    ax = fig.add_subplot(projection='3d')
+
+    # Create lines with different numbers of points (ragged arrays)
+    edges = [
+        [(0, 0, 0), (1, 1, 1), (2, 2, 2)],  # 3 points
+        [(0, 1, 0), (1, 2, 1)],             # 2 points
+        [(1, 0, 1), (2, 1, 2), (3, 2, 3), (4, 3, 4)]  # 4 points
+    ]
+
+    # This should not raise an exception.
+    collections = ax.add_collection3d(art3d.Line3DCollection(edges), autolim=True)
+
+    # Check that limits were computed correctly with margins
+    # The limits should include all points with default margins
+    assert np.allclose(ax.get_xlim3d(), (-0.08333333333333333, 4.083333333333333))
+    assert np.allclose(ax.get_ylim3d(), (-0.0625, 3.0625))
+    assert np.allclose(ax.get_zlim3d(), (-0.08333333333333333, 4.083333333333333))