Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63e0329

Browse files
MengAiDevQuLogic
andauthored
Fix Line3DCollection with autolim=True for lines of different lengths (#30423)
* Fix Line3DCollection with autolim=True for lines of different lengths When using Line3DCollection with autolim=True, the code was trying to convert ragged arrays (lines with different numbers of points) directly to a numpy array, which would fail. This fix extracts coordinates separately to avoid the issue. Fixes #30418 * fix lint * fix lint * Add test for Line3DCollection with autolim=True and ragged arrays * move test * Update lib/mpl_toolkits/mplot3d/tests/test_axes3d.py Co-authored-by: Elliott Sales de Andrade <[email protected]> * Update lib/mpl_toolkits/mplot3d/tests/test_axes3d.py Co-authored-by: Elliott Sales de Andrade <[email protected]> --------- Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent b78e4a1 commit 63e0329

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,8 +2890,10 @@ def add_collection3d(self, col, zs=0, zdir='z', autolim=True, *,
28902890

28912891
if autolim:
28922892
if isinstance(col, art3d.Line3DCollection):
2893-
self.auto_scale_xyz(*np.array(col._segments3d).transpose(),
2894-
had_data=had_data)
2893+
# Handle ragged arrays by extracting coordinates separately
2894+
all_points = np.concatenate(col._segments3d)
2895+
self.auto_scale_xyz(all_points[:, 0], all_points[:, 1],
2896+
all_points[:, 2], had_data=had_data)
28952897
elif isinstance(col, art3d.Poly3DCollection):
28962898
self.auto_scale_xyz(col._faces[..., 0],
28972899
col._faces[..., 1],

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,3 +2689,25 @@ def test_ndarray_color_kwargs_value_error():
26892689
ax = fig.add_subplot(111, projection='3d')
26902690
ax.scatter(1, 0, 0, color=np.array([0, 0, 0, 1]))
26912691
fig.canvas.draw()
2692+
2693+
2694+
def test_line3dcollection_autolim_ragged():
2695+
"""Test Line3DCollection with autolim=True and lines of different lengths."""
2696+
fig = plt.figure()
2697+
ax = fig.add_subplot(projection='3d')
2698+
2699+
# Create lines with different numbers of points (ragged arrays)
2700+
edges = [
2701+
[(0, 0, 0), (1, 1, 1), (2, 2, 2)], # 3 points
2702+
[(0, 1, 0), (1, 2, 1)], # 2 points
2703+
[(1, 0, 1), (2, 1, 2), (3, 2, 3), (4, 3, 4)] # 4 points
2704+
]
2705+
2706+
# This should not raise an exception.
2707+
collections = ax.add_collection3d(art3d.Line3DCollection(edges), autolim=True)
2708+
2709+
# Check that limits were computed correctly with margins
2710+
# The limits should include all points with default margins
2711+
assert np.allclose(ax.get_xlim3d(), (-0.08333333333333333, 4.083333333333333))
2712+
assert np.allclose(ax.get_ylim3d(), (-0.0625, 3.0625))
2713+
assert np.allclose(ax.get_zlim3d(), (-0.08333333333333333, 4.083333333333333))

0 commit comments

Comments
 (0)