Skip to content

Commit 0679da5

Browse files
committed
Expanded the empty input support and checks for a wider variety of plotting, especially plot_wireframe.
1 parent f62a947 commit 0679da5

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

lib/mpl_toolkits/mplot3d/art3d.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,13 @@ def get_vector(self, segments3d):
398398
ei = si+len(p)
399399
segis.append((si, ei))
400400
si = ei
401-
xs, ys, zs = zip(*points)
401+
402+
if len(segments3d) > 0 :
403+
xs, ys, zs = zip(*points)
404+
else :
405+
# We need this so that we can skip the bad unpacking from zip()
406+
xs, ys, zs = [], [], []
407+
402408
ones = np.ones(len(xs))
403409
self._vec = np.array([xs, ys, zs, ones])
404410
self._segis = segis

lib/mpl_toolkits/mplot3d/axes3d.py

+44-7
Original file line numberDiff line numberDiff line change
@@ -904,12 +904,37 @@ def plot_wireframe(self, X, Y, Z, *args, **kwargs):
904904
cstride = kwargs.pop("cstride", 1)
905905

906906
had_data = self.has_data()
907+
Z = np.atleast_2d(Z)
908+
# FIXME: Support masked arrays
909+
X = np.asarray(X)
910+
Y = np.asarray(Y)
907911
rows, cols = Z.shape
908-
912+
# Force X and Y to take the same shape.
913+
# If they can not be fitted to that shape,
914+
# then an exception is automatically thrown.
915+
X.shape = (rows, cols)
916+
Y.shape = (rows, cols)
917+
918+
# We want two sets of lines, one running along the "rows" of
919+
# Z and another set of lines running along the "columns" of Z.
920+
# This transpose will make it easy to obtain the columns.
909921
tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
910922

911-
rii = [i for i in range(0, rows, rstride)]+[rows-1]
912-
cii = [i for i in range(0, cols, cstride)]+[cols-1]
923+
rii = range(0, rows, rstride)
924+
cii = range(0, cols, cstride)
925+
926+
# Add the last index only if needed
927+
if rows > 0 and rii[-1] != (rows - 1) :
928+
rii += [rows-1]
929+
if cols > 0 and cii[-1] != (cols - 1) :
930+
cii += [cols-1]
931+
932+
# If the inputs were empty, then just
933+
# reset everything.
934+
if Z.size == 0 :
935+
rii = []
936+
cii = []
937+
913938
xlines = [X[i] for i in rii]
914939
ylines = [Y[i] for i in rii]
915940
zlines = [Z[i] for i in rii]
@@ -1145,16 +1170,21 @@ def add_collection3d(self, col, zs=0, zdir='z'):
11451170
- LineColleciton
11461171
- PatchCollection
11471172
'''
1173+
zvals = np.atleast_1d(zs)
1174+
if len(zvals) > 0 :
1175+
zsortval = min(zvals)
1176+
else :
1177+
zsortval = 0 # FIXME: Fairly arbitrary. Is there a better value?
11481178

11491179
if type(col) is collections.PolyCollection:
11501180
art3d.poly_collection_2d_to_3d(col, zs=zs, zdir=zdir)
1151-
col.set_sort_zpos(min(zs))
1181+
col.set_sort_zpos(zsortval)
11521182
elif type(col) is collections.LineCollection:
11531183
art3d.line_collection_2d_to_3d(col, zs=zs, zdir=zdir)
1154-
col.set_sort_zpos(min(zs))
1184+
col.set_sort_zpos(zsortval)
11551185
elif type(col) is collections.PatchCollection:
11561186
art3d.patch_collection_2d_to_3d(col, zs=zs, zdir=zdir)
1157-
col.set_sort_zpos(min(zs))
1187+
col.set_sort_zpos(zsortval)
11581188

11591189
Axes.add_collection(self, col)
11601190

@@ -1263,7 +1293,14 @@ def bar(self, left, height, zs=0, zdir='z', *args, **kwargs):
12631293
if 'alpha' in kwargs:
12641294
p.set_alpha(kwargs['alpha'])
12651295

1266-
xs, ys = zip(*verts)
1296+
if len(verts) > 0 :
1297+
# the following has to be skipped if verts is empty
1298+
# NOTE: Bugs could still occur if len(verts) > 0,
1299+
# but the "2nd dimension" is empty.
1300+
xs, ys = zip(*verts)
1301+
else :
1302+
xs, ys = [], []
1303+
12671304
xs, ys, verts_zs = art3d.juggle_axes(xs, ys, verts_zs, zdir)
12681305
self.auto_scale_xyz(xs, ys, verts_zs, had_data)
12691306

0 commit comments

Comments
 (0)