8000 Backport PR #25481: Fix 3D set_aspect error cases · matplotlib/matplotlib@871f7d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 871f7d9

Browse files
oscargusmeeseeksmachine
authored andcommitted
Backport PR #25481: Fix 3D set_aspect error cases
1 parent f451643 commit 871f7d9

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,8 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
337337
ptp = np.ptp(view_intervals, axis=1)
338338
if self._adjustable == 'datalim':
339339
mean = np.mean(view_intervals, axis=1)
340-
delta = max(ptp[ax_indices])
341-
scale = self._box_aspect[ptp == delta][0]
342-
deltas = delta * self._box_aspect / scale
340+
scale = max(ptp[ax_indices] / self._box_aspect[ax_indices])
341+
deltas = scale * self._box_aspect
343342

344343
for i, set_lim in enumerate((self.set_xlim3d,
345344
self.set_ylim3d,
Loading
Loading

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
image_comparison, remove_text=True, style='default')
2525

2626

27+
def plot_cuboid(ax, scale):
28+
# plot a rectangular cuboid with side lengths given by scale (x, y, z)
29+
r = [0, 1]
30+
pts = itertools.combinations(np.array(list(itertools.product(r, r, r))), 2)
31+
for start, end in pts:
32+
if np.sum(np.abs(start - end)) == r[1] - r[0]:
33+
ax.plot3D(*zip(start*np.array(scale), end*np.array(scale)))
34+
35+
2736
@check_figures_equal(extensions=["png"])
2837
def test_invisible_axes(fig_test, fig_ref):
2938
ax = fig_test.subplots(subplot_kw=dict(projection='3d'))
@@ -32,20 +41,19 @@ def test_invisible_axes(fig_test, fig_ref):
3241

3342
@mpl3d_image_comparison(['aspects.png'], remove_text=False)
3443
def test_aspects():
35-
aspects = ('auto', 'equal', 'equalxy', 'equalyz', 'equalxz')
36-
fig, axs = plt.subplots(1, len(aspects), subplot_kw={'projection': '3d'})
44+
aspects = ('auto', 'equal', 'equalxy', 'equalyz', 'equalxz', 'equal')
45+
_, axs = plt.subplots(2, 3, subplot_kw={'projection': '3d'})
3746

38-
# Draw rectangular cuboid with side lengths [1, 1, 5]
39-
r = [0, 1]
40-
scale = np.array([1, 1, 5])
41-
pts = itertools.combinations(np.array(list(itertools.product(r, r, r))), 2)
42-
for start, end in pts:
43-
if np.sum(np.abs(start - end)) == r[1] - r[0]:
44-
for ax in axs:
45-
ax.plot3D(*zip(start*scale, end*scale))
46-
for i, ax in enumerate(axs):
47+
for ax in axs.flatten()[0:-1]:
48+
plot_cuboid(ax, scale=[1, 1, 5])
49+
# plot a cube as well to cover github #25443
50+
plot_cuboid(axs[1][2], scale=[1, 1, 1])
51+
52+
for i, ax in enumerate(axs.flatten()):
53+
ax.set_title(aspects[i])
4754
ax.set_box_aspect((3, 4, 5))
4855
ax.set_aspect(aspects[i], adjustable='datalim')
56+
axs[1][2].set_title('equal (cube)')
4957

5058

5159
@mpl3d_image_comparison(['aspects_adjust_box.png'], remove_text=False)
@@ -54,15 +62,9 @@ def test_aspects_adjust_box():
5462
fig, axs = plt.subplots(1, len(aspects), subplot_kw={'projection': '3d'},
5563
figsize=(11, 3))
5664

57-
# Draw rectangular cuboid with side lengths [4, 3, 5]
58-
r = [0, 1]
59-
scale = np.array([4, 3, 5])
60-
pts = itertools.combinations(np.array(list(itertools.product(r, r, r))), 2)
61-
for start, end in pts:
62-
if np.sum(np.abs(start - end)) == r[1] - r[0]:
63-
for ax in axs:
64-
ax.plot3D(*zip(start*scale, end*scale))
6565
for i, ax in enumerate(axs):
66+
plot_cuboid(ax, scale=[4, 3, 5])
67+
ax.set_title(aspects[i])
6668
ax.set_aspect(aspects[i], adjustable='box')
6769

6870

0 commit comments

Comments
 (0)
0