8000 FIX: Autoscale support in add_collection3d for Line3DCollection and P… · matplotlib/matplotlib@7ee56ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ee56ce

Browse files
FIX: Autoscale support in add_collection3d for Line3DCollection and Poly3DCollection (#28403)
* Autoscale Line3DCollection and Poly3DCollection
1 parent e243b2c commit 7ee56ce

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

galleries/examples/mplot3d/polys3d.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
poly = Poly3DCollection(verts, alpha=.7)
3232
ax.add_collection3d(poly)
33-
ax.auto_scale_xyz(verts[:, :, 0], verts[:, :, 1], verts[:, :, 2])
3433
ax.set_aspect('equalxy')
3534

3635
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

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

2741-
def add_collection3d(self, col, zs=0, zdir='z'):
2741+
def add_collection3d(self, col, zs=0, zdir='z', autolim=True):
27422742
"""
27432743
Add a 3D collection object to the plot.
27442744
@@ -2750,8 +2750,21 @@ def add_collection3d(self, col, zs=0, zdir='z'):
27502750
27512751
- `.PolyCollection`
27522752
- `.LineCollection`
2753-
- `.PatchCollection`
2753+
- `.PatchCollection` (currently not supporting *autolim*)
2754+
2755+
Parameters
2756+
----------
2757+
col : `.Collection`
2758+
A 2D collection object.
2759+
zs : float or array-like, default: 0
2760+
The z-positions to be used for the 2D objects.
2761+
zdir : {'x', 'y', 'z'}, default: 'z'
2762+
The direction to use for the z-positions.
2763+
autolim : bool, default: True
2764+
Whether to update the data limits.
27542765
"""
2766+
had_data = self.has_data()
2767+
27552768
zvals = np.atleast_1d(zs)
27562769
zsortval = (np.min(zvals) if zvals.size
27572770
else 0) # FIXME: arbitrary default
@@ -2769,6 +2782,18 @@ def add_collection3d(self, col, zs=0, zdir='z'):
27692782
art3d.patch_collection_2d_to_3d(col, zs=zs, zdir=zdir)
27702783
col.set_sort_zpos(zsortval)
27712784

2785+
if autolim:
2786+
if isinstance(col, art3d.Line3DCollection):
2787+
self.auto_scale_xyz(*np.array(col._segments3d).transpose(),
2788+
had_data=had_data)
2789+
elif isinstance(col, art3d.Poly3DCollection):
2790+
self.auto_scale_xyz(*col._vec[:-1], had_data=had_data)
2791+
elif isinstance(col, art3d.Patch3DCollection):
2792+
pass
2793+
# FIXME: Implement auto-scaling function for Patch3DCollection
2794+
# Currently unable to do so due to issues with Patch3DCollection
2795+
# See https://github.com/matplotlib/matplotlib/issues/14298 for details
2796+
27722797
collection = super().add_collection(col)
27732798
return collection
27742799

Loading

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,8 @@ def test_poly3dcollection_closed():
10041004
facecolor=(0.5, 0.5, 1, 0.5), closed=True)
10051005
c2 = art3d.Poly3DCollection([poly2], linewidths=3, edgecolor='k',
10061006
facecolor=(1, 0.5, 0.5, 0.5), closed=False)
1007-
ax.add_collection3d(c1)
1008-
ax.add_collection3d(c2)
1007+
ax.add_collection3d(c1, autolim=False)
1008+
ax.add_collection3d(c2, autolim=False)
10091009

10101010

10111011
def test_poly_collection_2d_to_3d_empty():
@@ -1038,8 +1038,8 @@ def test_poly3dcollection_alpha():
10381038
c2.set_facecolor((1, 0.5, 0.5))
10391039
c2.set_edgecolor('k')
10401040
c2.set_alpha(0.5)
1041-
ax.add_collection3d(c1)
1042-
ax.add_collection3d(c2)
1041+
ax.add_collection3d(c1, autolim=False)
1042+
ax.add_collection3d(c2, autolim=False)
10431043

10441044

10451045
@mpl3d_image_comparison(['add_collection3d_zs_array.png'], style='mpl20')
@@ -1098,6 +1098,32 @@ def test_add_collection3d_zs_scalar():
10981098
ax.set_zlim(0, 2)
10991099

11001100

1101+
def test_line3dCollection_autoscaling():
1102+
fig = plt.figure()
1103+
ax = fig.add_subplot(projection='3d')
1104+
1105+
lines = [[(0, 0, 0), (1, 4, 2)],
1106+
[(1, 1, 3), (2, 0, 2)],
1107+
[(1, 0, 4), (1, 4, 5)]]
1108+
1109+
lc = art3d.Line3DCollection(lines)
1110+
ax.add_collection3d(lc)
1111+
assert np.allclose(ax.get_xlim3d(), (-0.041666666666666664, 2.0416666666666665))
1112+
assert np.allclose(ax.get_ylim3d(), (-0.08333333333333333, 4.083333333333333))
1113+
assert np.allclose(ax.get_zlim3d(), (-0.10416666666666666, 5.104166666666667))
1114+
1115+
1116+
def test_poly3dCollection_autoscaling():
1117+
fig = plt.figure()
1118+
ax = fig.add_subplot(projection='3d')
1119+
poly = np.array([[0, 0, 0], [1, 1, 3], [1, 0, 4]])
1120+
col = art3d.Poly3DCollection([poly])
1121+
ax.add_collection3d(col)
1122+
assert np.allclose(ax.get_xlim3d(), (-0.020833333333333332, 1.0208333333333333))
1123+
assert np.allclose(ax.get_ylim3d(), (-0.020833333333333332, 1.0208333333333333))
1124+
assert np.allclose(ax.get_zlim3d(), (-0.0833333333333333, 4.083333333333333))
1125+
1126+
11011127
@mpl3d_image_comparison(['axes3d_labelpad.png'],
11021128
remove_text=False, style='mpl20')
11031129
def test_axes3d_labelpad():

0 commit comments

Comments
 (0)
0