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

Skip to content

Commit 5f2b890

Browse files
authored
Merge pull request #19399 from QuLogic/fix-box3d
FIX: empty Poly3DCollections
2 parents b93f563 + 7facd3f commit 5f2b890

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -866,15 +866,21 @@ def do_3d_projection(self, renderer=None):
866866
else:
867867
cedge = cedge.repeat(len(xyzlist), axis=0)
868868

869-
# sort by depth (furthest drawn first)
870-
z_segments_2d = sorted(
871-
((self._zsortfunc(zs), np.column_stack([xs, ys]), fc, ec, idx)
872-
for idx, ((xs, ys, zs), fc, ec)
873-
in enumerate(zip(xyzlist, cface, cedge))),
874-
key=lambda x: x[0], reverse=True)
875-
876-
zzs, segments_2d, self._facecolors2d, self._edgecolors2d, idxs = \
877-
zip(*z_segments_2d)
869+
if xyzlist:
870+
# sort by depth (furthest drawn first)
871+
z_segments_2d = sorted(
872+
((self._zsortfunc(zs), np.column_stack([xs, ys]), fc, ec, idx)
873+
for idx, ((xs, ys, zs), fc, ec)
874+
in enumerate(zip(xyzlist, cface, cedge))),
875+
key=lambda x: x[0], reverse=True)
876+
877+
_, segments_2d, self._facecolors2d, self._edgecolors2d, idxs = \
878+
zip(*z_segments_2d)
879+
else:
880+
segments_2d = []
881+
self._facecolors2d = np.empty((0, 4))
882+
self._edgecolors2d = np.empty((0, 4))
883+
idxs = []
878884

879885
if self._codes3d is not None:
880886
codes = [self._codes3d[idx] for idx in idxs]

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ def test_scatter3d():
233233
x = y = z = np.arange(10, 20)
234234
ax.scatter(x, y, z, c='b', marker='^')
235235
z[-1] = 0 # Check that scatter() copies the data.
236+
# Ensure empty scatters do not break.
237+
ax.scatter([], [], [], c='r', marker='X')
236238

237239

238240
@mpl3d_image_comparison(['scatter3d_color.png'])
@@ -625,6 +627,14 @@ def test_poly_collection_2d_to_3d_empty():
625627
assert isinstance(poly, art3d.Poly3DCollection)
626628
assert poly.get_paths() == []
627629

630+
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
631+
ax.add_artist(poly)
632+
minz = poly.do_3d_projection()
633+
assert np.isnan(minz)
634+
635+
# Ensure drawing actually works.
636+
fig.canvas.draw()
637+
628638

629639
@mpl3d_image_comparison(['poly3dcollection_alpha.png'])
630640
def test_poly3dcollection_alpha():

0 commit comments

Comments
 (0)