Skip to content

Commit 7211708

Browse files
committed
Removing _juggle_axes_vec and _rotate_axes_vec
1 parent e43b930 commit 7211708

File tree

2 files changed

+62
-71
lines changed

2 files changed

+62
-71
lines changed

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 54 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ def set_3d_properties(self, zs=0, zdir='z'):
160160
ys = self.get_ydata()
161161

162162
zs = np.broadcast_to(zs, len(xs))
163-
xyz = np.asarray([xs, ys, zs])
164-
self._verts3d = _juggle_axes_vec(xyz, zdir)
163+
self._verts3d = juggle_axes(xs, ys, zs, zdir)
165164
self.stale = True
166165

167166
@artist.allow_rasterization
@@ -181,52 +180,58 @@ def line_2d_to_3d(line, zs=0, zdir='z'):
181180

182181

183182
def path_to_3d_segment(path, zs=0, zdir='z'):
184-
"""Convert a path to a 3D segment."""
185-
seg3d = np.empty((3, len(path)))
186-
# Works both if zs is an array or a scalar
187-
seg3d[2, :] = zs
183+
"""Convert a path to a 3D segment.
184+
185+
.. versionchanged :: 3.1
186+
Return type changed from a list to a numpy.array
187+
"""
188+
zs = np.broadcast_to(zs, len(path))
188189

189190
pathsegs = path.iter_segments(simplify=False, curves=False)
190-
for i, ((x, y), code) in enumerate(pathsegs):
191-
seg3d[0, i] = x
192-
seg3d[1, i] = y
191+
if len(path):
192+
xs, ys = zip(*((x, y) for (x, y), code in pathsegs))
193+
else:
194+
xs, ys = [], []
193195

194-
seg3d = _juggle_axes_vec(seg3d, zdir)
195-
return seg3d.T
196+
seg3d = juggle_axes(xs, ys, zs, zdir)
197+
return np.array(seg3d).T
196198

197199

198200
def paths_to_3d_segments(paths, zs=0, zdir='z'):
199201
"""Convert paths from a collection object to 3D segments.
200202
201-
.. versionchanged :: 1.3.1
202-
This function returns a numpy array instead of a list
203+
.. versionchanged :: 3.1
204+
Return type changed from a list to a numpy.array
203205
"""
204206

205207
zs = np.broadcast_to(zs, len(paths))
208+
206209
segs = [path_to_3d_segment(path, pathz, zdir)
207210
for path, pathz in zip(paths, zs)]
208-
return np.asarray(segs)
211+
return segs
209212

210213

211214
def path_to_3d_segment_with_codes(path, zs=0, zdir='z'):
212-
"""Convert a path to a 3D segment with path codes."""
213-
seg3d = np.empty((3, len(path)))
214-
# Works both if zs is an array and a scalar
215-
seg3d[2, :] = zs
215+
"""Convert a path to a 3D segment with path codes.
216+
217+
.. versionchanged :: 3.1
218+
Return type changed from a list to a numpy.array
219+
"""
220+
zs = np.broadcast_to(zs, len(path))
216221

217222
pathsegs = path.iter_segments(simplify=False, curves=False)
218223
codes = np.empty(len(path))
219-
for i, ((x, y), code) in enumerate(pathsegs):
220-
seg3d[0, i] = x
221-
seg3d[1, i] = y
222-
codes[i] = code
223-
seg3d = _juggle_axes_vec(seg3d, zdir)
224-
return seg3d.T, codes
224+
xs, ys, codes = zip(*((x, y, code) for (x, y), code in pathsegs))
225+
seg3d = juggle_axes(xs, ys, zs, zdir)
226+
return np.array(seg3d).T, codes
225227

226228

227229
def paths_to_3d_segments_with_codes(paths, zs=0, zdir='z'):
228230
"""
229231
Convert paths from a collection object to 3D segments with path codes.
232+
233+
.. versionchanged :: 3.1
234+
Return type changed from a list to a numpy.array
230235
"""
231236

232237
zs = np.broadcast_to(zs, len(paths))
@@ -309,8 +314,8 @@ def __init__(self, *args, zs=(), zdir='z', **kwargs):
309314

310315
def set_3d_properties(self, verts, zs=0, zdir='z'):
311316
zs = np.broadcast_to(zs, len(verts))
312-
verts = np.hstack([verts, zs[:, None]])
313-
self._segment3d = _juggle_axes_vec(verts.T, zdir)
317+
xs, ys = verts.T
318+
self._segment3d = juggle_axes(xs, ys, zs, zdir)
314319
self._facecolor3d = Patch.get_facecolor(self)
315320

316321
def get_path(self):
@@ -321,7 +326,11 @@ def get_facecolor(self):
321326

322327
def do_3d_projection(self, renderer):
323328
# pad ones
324-
s = np.vstack([self._segment3d, np.ones(self._segment3d.shape[1])])
329+
if self._segment3d:
330+
segments_3d_len = len(self._segment3d[0])
331+
else:
332+
segments_3d_len = 0
333+
s = np.vstack(self._segment3d + (np.ones(segments_3d_len),))
325334
vxyzis = proj3d.proj_transform_vec_clip(s, renderer.M)
326335
self._path2d = mpath.Path(vxyzis[0:2].T)
327336
# FIXME: coloring
@@ -344,7 +353,11 @@ def set_3d_properties(self, path, zs=0, zdir='z'):
344353

345354
def do_3d_projection(self, renderer):
346355
# pad ones
347-
s = np.vstack([self._segment3d, np.ones(self._segment3d.shape[1])])
356+
if self._segment3d:
357+
segments_3d_len = len(self._segment3d[0])
358+
else:
359+
segments_3d_len = 0
360+
s = np.vstack(self._segment3d + (np.ones(segments_3d_len),))
348361
vxyzis = proj3d.proj_transform_vec_clip(s, renderer.M)
349362
self._path2d = mpath.Path(vxyzis[0:2].T, self._code3d)
350363
# FIXME: coloring
@@ -414,8 +427,13 @@ def set_3d_properties(self, zs, zdir):
414427
# Force the collection to initialize the face and edgecolors
415428
# just in case it is a scalarmappable with a colormap.
416429
self.update_scalarmappable()
417-
offsets = np.vstack(self.get_offsets(), np.atleast_1d(zs))
418-
self._offsets3d = _juggle_axes_vec(offsets, zdir)
430+
offsets = self.get_offsets()
431+
if len(offsets) > 0:
432+
xs, ys = offsets.T
433+
else:
434+
xs = []
435+
ys = []
436+
self._offsets3d = juggle_axes(xs, ys, np.atleast_1d(zs), zdir)
419437
self._facecolor3d = self.get_facecolor()
420438
self._edgecolor3d = self.get_edgecolor()
421439
self.stale = True
@@ -478,9 +496,12 @@ def set_3d_properties(self, zs, zdir):
478496
# just in case it is a scalarmappable with a colormap.
479497
self.update_scalarmappable()
480498
offsets = self.get_offsets()
481-
offsets = np.hstack([offsets,
482-
np.broadcast_to(zs, len(offsets))[:, np.newaxis]])
483-
self._offsets3d = _juggle_axes_vec(offsets, zdir).T
499+
if len(offsets) > 0:
500+
xs, ys = offsets.T
501+
else:
502+
xs = []
503+
ys = []
504+
self._offsets3d = juggle_axes(xs, ys, np.atleast_1d(zs), zdir)
484505
self._facecolor3d = self.get_facecolor()
485506
self._edgecolor3d = self.get_edgecolor()
486507
self.stale = True
@@ -757,22 +778,6 @@ def juggle_axes(xs, ys, zs, zdir):
757778
return xs, ys, zs
758779

759780

760-
def _juggle_axes_vec(xyz, zdir):
761-
"""
762-
Reorder coordinates so that 2D xs, ys can be plotted in the plane
763-
orthogonal to zdir. zdir is normally x, y or z. However, if zdir
764-
starts with a '-' it is interpreted as a compensation for rotate_axes.
765-
"""
766-
if zdir == 'x':
767-
return xyz[[2, 0, 1]]
768-
elif zdir == 'y':
769-
return xyz[[0, 2, 1]]
770-
elif zdir.startswith('-'):
771-
return _rotate_axes_vec(xyz, zdir)
772-
else:
773-
return xyz
774-
775-
776781
def rotate_axes(xs, ys, zs, zdir):
777782
"""
778783
Reorder coordinates so that the axes are rotated with zdir along
@@ -791,26 +796,6 @@ def rotate_axes(xs, ys, zs, zdir):
791796
return xs, ys, zs
792797

793798

794-
def _rotate_axes_vec(xyz, zdir):
795-
"""
796-
Reorder coordinates so that the axes are rotated with zdir along
797-
the original z axis. Prepending the axis with a '-' does the
798-
inverse transform, so zdir can be x, -x, y, -y, z or -z
799-
"""
800-
if zdir == 'x':
801-
return xyz[[1, 2, 0]]
802-
elif zdir == '-x':
803-
return xyz[[2, 0, 1]]
804-
805-
elif zdir == 'y':
806-
return xyz[[2, 0, 1]]
807-
elif zdir == '-y':
808-
return xyz[[1, 2, 0]]
809-
810-
else:
811-
return xyz
812-
813-
814799
def get_colors(c, num):
815800
"""Stretch the color argument to provide the required number *num*."""
816801
if not len(c):

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,9 +2344,15 @@ def bar(self, left, height, zs=0, zdir='z', *args, **kwargs):
23442344
if 'alpha' in kwargs:
23452345
p.set_alpha(kwargs['alpha'])
23462346

2347-
verts = np.vstack([list(zip(*verts)), verts_zs])
2347+
if len(verts) > 0:
2348+
# the following has to be skipped if verts is empty
2349+
# NOTE: Bugs could still occur if len(verts) > 0,
2350+
# but the "2nd dimension" is empty.
2351+
xs, ys = zip(*verts)
2352+
else:
2353+
xs, ys = [], []
23482354

2349-
xs, ys, verts_zs = art3d._juggle_axes_vec(verts, zdir)
2355+
xs, ys, verts_zs = art3d.juggle_axes(xs, ys, verts_zs, zdir)
23502356
self.auto_scale_xyz(xs, ys, verts_zs, had_data)
23512357

23522358
return patches

0 commit comments

Comments
 (0)