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

Skip to content

Commit 69ea30c

Browse files
Autoscale Line3DCollection and Poly3DCollection
1 parent 5b36d02 commit 69ea30c

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ def tricontourf(self, *args, zdir='z', offset=None, **kwargs):
26142614
self._auto_scale_contourf(X, Y, Z, zdir, levels, had_data)
26152615
return cset
26162616

2617-
def add_collection3d(self, col, zs=0, zdir='z'):
2617+
def add_collection3d(self, col, zs=0, zdir='z', autolim=True):
26182618
"""
26192619
Add a 3D collection object to the plot.
26202620
@@ -2627,7 +2627,20 @@ def add_collection3d(self, col, zs=0, zdir='z'):
26272627
- `.PolyCollection`
26282628
- `.LineCollection`
26292629
- `.PatchCollection`
2630+
2631+
Parameters
2632+
----------
2633+
col : `.Collection`
2634+
A 2D collection object.
2635+
zs : float or array-like, default: 0
2636+
The z-positions to be used for the 2D objects.
2637+
zdir : {'x', 'y', 'z'}, default: 'z'
2638+
The direction to use for the z-positions.
2639+
autolim : bool, default: True
2640+
Whether to update the data limits.
26302641
"""
2642+
had_data = self.has_data()
2643+
26312644
zvals = np.atleast_1d(zs)
26322645
zsortval = (np.min(zvals) if zvals.size
26332646
else 0) # FIXME: arbitrary default
@@ -2645,6 +2658,16 @@ def add_collection3d(self, col, zs=0, zdir='z'):
26452658
art3d.patch_collection_2d_to_3d(col, zs=zs, zdir=zdir)
26462659
col.set_sort_zpos(zsortval)
26472660

2661+
# FIXME: Implement auto-scaling function for Patch3DCollection
2662+
# Currently unable to do so due to issues with Patch3DCollection
2663+
# See https://github.com/matplotlib/matplotlib/issues/14298 for details
2664+
if autolim:
2665+
if isinstance(col, art3d.Line3DCollection):
2666+
self.auto_scale_xyz(*np.array(col._segments3d).transpose(), had_data=had_data)
2667+
2668+
if isinstance(col, art3d.Poly3DCollection):
2669+
self.auto_scale_xyz(*col._vec[:-1], had_data=had_data)
2670+
26482671
collection = super().add_collection(col)
26492672
return collection
26502673

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

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

968968

969969
def test_poly_collection_2d_to_3d_empty():
@@ -996,8 +996,8 @@ def test_poly3dcollection_alpha():
996996
c2.set_facecolor((1, 0.5, 0.5))
997997
c2.set_edgecolor('k')
998998
c2.set_alpha(0.5)
999-
ax.add_collection3d(c1)
1000-
ax.add_collection3d(c2)
999+
ax.add_collection3d(c1, autolim=False)
1000+
ax.add_collection3d(c2, autolim=False)
10011001

10021002

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

10581058

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

0 commit comments

Comments
 (0)