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

Skip to content

Commit 64f197b

Browse files
committed
BUG: Fix the aspect ratio of 3d plots
Fixes gh-8894, by always using a "position" that maintains a uniform coordinate system. Test added - when viewed from above, the plot should be square not rhombic.
1 parent 5b7ccfe commit 64f197b

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``Axes3D`` now preserves right angles when rotating
2+
---------------------------------------------------
3+
4+
:class:`~mpl_toolkits.mplot3d.Axes3D` no longer stretches the plot in the x
5+
axis after projecting.

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,9 +1467,9 @@ def apply_aspect(self, position=None):
14671467
"""
14681468
Adjust the Axes for a specified data aspect ratio.
14691469
1470-
Depending on `.get_adjustable` this will modify either the Axes box
1471-
(position) or the view limits. In the former case, `.get_anchor`
1472-
will affect the position.
1470+
Depending on `.get_adjustable` this will modify either the
1471+
Axes box (position) or the view limits. In the former case,
1472+
`~matplotlib.axes.Axes.get_anchor` will affect the position.
14731473
14741474
Notes
14751475
-----

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@ def tunit_edges(self, vals=None, M=None):
265265
(tc[7], tc[4])]
266266
return edges
267267

268+
def apply_aspect(self, position=None):
269+
if position is None:
270+
position = self.get_position(original=True)
271+
272+
# in the superclass, we would go through and actually deal with axis
273+
# scales and box/datalim. Those are all irrelevant - all we need to do
274+
# is make sure our coordinate system is square.
275+
figW, figH = self.get_figure().get_size_inches()
276+
fig_aspect = figH / figW
277+
box_aspect = 1
278+
pb = position.frozen()
279+
pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect)
280+
self.set_position(pb1.anchored(self.get_anchor(), pb), 'active')
281+
268282
@artist.allow_rasterization
269283
def draw(self, renderer):
270284
# draw the background patch

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,13 @@ def test_axes3d_cla():
478478
ax.cla() # make sure the axis displayed is 3D (not 2D)
479479

480480

481+
@image_comparison(['axes3d_rotated.png'])
482+
def test_axes3d_rotated():
483+
fig = plt.figure()
484+
ax = fig.add_subplot(1, 1, 1, projection='3d')
485+
ax.view_init(90, 45) # look down, rotated. Should be square
486+
487+
481488
def test_plotsurface_1d_raises():
482489
x = np.linspace(0.5, 10, num=100)
483490
y = np.linspace(0.5, 10, num=100)

0 commit comments

Comments
 (0)