Skip to content

Commit c747ac4

Browse files
Backport PR #28403: FIX: Autoscale support in add_collection3d for Line3DCollection and Poly3DCollection
1 parent b0dba39 commit c747ac4

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,7 +2578,7 @@ def tricontourf(self, *args, zdir='z', offset=None, **kwargs):
25782578
self._auto_scale_contourf(X, Y, Z, zdir, levels, had_data)
25792579
return cset
25802580

2581-
def add_collection3d(self, col, zs=0, zdir='z'):
2581+
def add_collection3d(self, col, zs=0, zdir='z', autolim=True):
25822582
"""
25832583
Add a 3D collection object to the plot.
25842584
@@ -2590,8 +2590,21 @@ def add_collection3d(self, col, zs=0, zdir='z'):
25902590
25912591
- `.PolyCollection`
25922592
- `.LineCollection`
2593-
- `.PatchCollection`
2593+
- `.PatchCollection` (currently not supporting *autolim*)
2594+
2595+
Parameters
2596+
----------
2597+
col : `.Collection`
2598+
A 2D collection object.
2599+
zs : float or array-like, default: 0
2600+
The z-positions to be used for the 2D objects.
2601+
zdir : {'x', 'y', 'z'}, default: 'z'
2602+
The direction to use for the z-positions.
2603+
autolim : bool, default: True
2604+
Whether to update the data limits.
25942605
"""
2606+
had_data = self.has_data()
2607+
25952608
zvals = np.atleast_1d(zs)
25962609
zsortval = (np.min(zvals) if zvals.size
25972610
else 0) # FIXME: arbitrary default
@@ -2609,6 +2622,18 @@ def add_collection3d(self, col, zs=0, zdir='z'):
26092622
art3d.patch_collection_2d_to_3d(col, zs=zs, zdir=zdir)
26102623
col.set_sort_zpos(zsortval)
26112624

2625+
if autolim:
2626+
if isinstance(col, art3d.Line3DCollection):
2627+
self.auto_scale_xyz(*np.array(col._segments3d).transpose(),
2628+
had_data=had_data)
2629+
elif isinstance(col, art3d.Poly3DCollection):
2630+
self.auto_scale_xyz(*col._vec[:-1], had_data=had_data)
2631+
elif isinstance(col, art3d.Patch3DCollection):
2632+
pass
2633+
# FIXME: Implement auto-scaling function for Patch3DCollection
2634+
# Currently unable to do so due to issues with Patch3DCollection
2635+
# See https://github.com/matplotlib/matplotlib/issues/14298 for details
2636+
26122637
collection = super().add_collection(col)
26132638
return collection
26142639

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,8 @@ def test_poly3dcollection_closed():
961961
facecolor=(0.5, 0.5, 1, 0.5), closed=True)
962962
c2 = art3d.Poly3DCollection([poly2], linewidths=3, edgecolor='k',
963963
facecolor=(1, 0.5, 0.5, 0.5), closed=False)
964-
ax.add_collection3d(c1)
965-
ax.add_collection3d(c2)
964+
ax.add_collection3d(c1, autolim=False)
965+
ax.add_collection3d(c2, autolim=False)
966966

967967

968968
def test_poly_collection_2d_to_3d_empty():
@@ -995,8 +995,8 @@ def test_poly3dcollection_alpha():
995995
c2.set_facecolor((1, 0.5, 0.5))
996996
c2.set_edgecolor('k')
997997
c2.set_alpha(0.5)
998-
ax.add_collection3d(c1)
999-
ax.add_collection3d(c2)
998+
ax.add_collection3d(c1, autolim=False)
999+
ax.add_collection3d(c2, autolim=False)
10001000

10011001

10021002
@mpl3d_image_comparison(['add_collection3d_zs_array.png'], style='mpl20')
@@ -1055,6 +1055,32 @@ def test_add_collection3d_zs_scalar():
10551055
ax.set_zlim(0, 2)
10561056

10571057

1058+
def test_line3dCollection_autoscaling():
1059+
fig = plt.figure()
1060+
ax = fig.add_subplot(projection='3d')
1061+
1062+
lines = [[(0, 0, 0), (1, 4, 2)],
1063+
[(1, 1, 3), (2, 0, 2)],
1064+
[(1, 0, 4), (1, 4, 5)]]
1065+
1066+
lc = art3d.Line3DCollection(lines)
1067+
ax.add_collection3d(lc)
1068+
assert np.allclose(ax.get_xlim3d(), (-0.041666666666666664, 2.0416666666666665))
1069+
assert np.allclose(ax.get_ylim3d(), (-0.08333333333333333, 4.083333333333333))
1070+
assert np.allclose(ax.get_zlim3d(), (-0.10416666666666666, 5.104166666666667))
1071+
1072+
1073+
def test_poly3dCollection_autoscaling():
1074+
fig = plt.figure()
1075+
ax = fig.add_subplot(projection='3d')
1076+
poly = np.array([[0, 0, 0], [1, 1, 3], [1, 0, 4]])
1077+
col = art3d.Poly3DCollection([poly])
1078+
ax.add_collection3d(col)
1079+
assert np.allclose(ax.get_xlim3d(), (-0.020833333333333332, 1.0208333333333333))
1080+
assert np.allclose(ax.get_ylim3d(), (-0.020833333333333332, 1.0208333333333333))
1081+
assert np.allclose(ax.get_zlim3d(), (-0.0833333333333333, 4.083333333333333))
1082+
1083+
10581084
@mpl3d_image_comparison(['axes3d_labelpad.png'],
10591085
remove_text=False, style='mpl20')
10601086
def test_axes3d_labelpad():

0 commit comments

Comments
 (0)