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

Skip to content

Commit 3cdbfa0

Browse files
Add equalxy, equalyz, equalxz aspect ratios
1 parent c8fc673 commit 3cdbfa0

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -315,25 +315,33 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
315315
--------
316316
mpl_toolkits.mplot3d.axes3d.Axes3D.set_box_aspect
317317
"""
318-
if aspect not in ('auto', 'equal'):
319-
raise NotImplementedError(
320-
"Axes3D currently only supports the aspect argument "
321-
f"'auto' or 'equal'. You passed in {aspect!r}."
322-
)
318+
_api.check_in_list(('auto', 'equal', 'equalxy', 'equalyz', 'equalxz'),
319+
aspect=aspect)
323320
super().set_aspect(
324-
aspect, adjustable=adjustable, anchor=anchor, share=share)
325-
326-
if aspect == 'equal':
327-
v_intervals = np.vstack((self.xaxis.get_view_interval(),
328-
self.yaxis.get_view_interval(),
329-
self.zaxis.get_view_interval()))
330-
mean = np.mean(v_intervals, axis=1)
331-
delta = np.max(np.ptp(v_intervals, axis=1))
321+
aspect='auto', adjustable=adjustable, anchor=anchor, share=share)
322+
323+
if aspect in ('equal', 'equalxy', 'equalxz', 'equalyz'):
324+
if aspect == 'equal':
325+
axis_indices = [0, 1, 2]
326+
elif aspect == 'equalxy':
327+
axis_indices = [0, 1]
328+
elif aspect == 'equalxz':
329+
axis_indices = [0, 2]
330+
elif aspect == 'equalyz':
331+
axis_indices = [1, 2]
332+
333+
view_intervals = np.array([self.xaxis.get_view_interval(),
334+
self.yaxis.get_view_interval(),
335+
self.zaxis.get_view_interval()])
336+
mean = np.mean(view_intervals, axis=1)
337+
delta = np.max(np.ptp(view_intervals, axis=1))
332338
deltas = delta * self._box_aspect / min(self._box_aspect)
333339

334-
self.set_xlim3d(mean[0] - deltas[0] / 2., mean[0] + deltas[0] / 2.)
335-
self.set_ylim3d(mean[1] - deltas[1] / 2., mean[1] + deltas[1] / 2.)
336-
self.set_zlim3d(mean[2] - deltas[2] / 2., mean[2] + deltas[2] / 2.)
340+
for i, set_lim in enumerate((self.set_xlim3d,
341+
self.set_ylim3d,
342+
self.set_zlim3d)):
343+
if i in axis_indices:
344+
set_lim(mean[i] - deltas[i]/2., mean[i] + deltas[i]/2.)
337345

338346
def set_box_aspect(self, aspect, *, zoom=1):
339347
"""

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,22 @@ def test_invisible_axes(fig_test, fig_ref):
2727
ax.set_visible(False)
2828

2929

30-
@mpl3d_image_comparison(['aspect_equal.png'], remove_text=False)
31-
def test_aspect_equal():
32-
fig = plt.figure()
33-
ax = fig.add_subplot(projection='3d')
34-
35-
points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0],
36-
[0, 0, 3], [1, 0, 3], [0, 1, 3], [1, 1, 3]])
37-
38-
x = np.asarray([coord[0] for coord in points])
39-
y = np.asarray([coord[1] for coord in points])
40-
z = np.asarray([coord[2] for coord in points])
41-
42-
def plot_edge(i, j):
43-
ax.plot([x[i], x[j]], [y[i], y[j]], [z[i], z[j]], c='r')
44-
45-
# hexaedron creation
46-
plot_edge(0, 1), plot_edge(1, 3), plot_edge(3, 2), plot_edge(2, 0)
47-
plot_edge(4, 5), plot_edge(5, 7), plot_edge(7, 6), plot_edge(6, 4)
48-
plot_edge(0, 4), plot_edge(1, 5), plot_edge(3, 7), plot_edge(2, 6)
49-
50-
ax.set_aspect('equal')
30+
@mpl3d_image_comparison(['aspects.png'], remove_text=False)
31+
def test_aspects():
32+
aspects = ('auto', 'equal', 'equalxy', 'equalyz', 'equalxz')
33+
fig, axs = plt.subplots(1, len(aspects), subplot_kw={'projection': '3d'})
34+
35+
# Draw rectangular cuboid with side lengths [1, 1, 2]
36+
r = [0, 1]
37+
scale = np.array([1, 1, 2])
38+
pts = itertools.combinations(np.array(list(itertools.product(r, r, r))), 2)
39+
for start, end in pts:
40+
if np.sum(np.abs(start - end)) == r[1] - r[0]:
41+
for ax in axs:
42+
ax.plot3D(*zip(start*scale, end*scale))
43+
for i, ax in enumerate(axs):
44+
ax.set_box_aspect((3, 4, 5))
45+
ax.set_aspect(aspects[i])
5146

5247

5348
@mpl3d_image_comparison(['bar3d.png'])

0 commit comments

Comments
 (0)