Skip to content

Commit 3d3b42e

Browse files
committed
BUG: Fix the aspect ratio of 3d plots
Fixes matplotlibgh-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 e4839b7 commit 3d3b42e

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

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
@@ -1441,9 +1441,9 @@ def apply_aspect(self, position=None):
14411441
"""
14421442
Adjust the Axes for a specified data aspect ratio.
14431443
1444-
Depending on `.get_adjustable` this will modify either the Axes box
1445-
(position) or the view limits. In the former case, `.get_anchor`
1446-
will affect the position.
1444+
Depending on `.get_adjustable` this will modify either the
1445+
Axes box (position) or the view limits. In the former case,
1446+
`~matplotlib.axes.Axes.get_anchor` will affect the position.
14471447
14481448
Notes
14491449
-----

‎lib/mpl_toolkits/mplot3d/axes3d.py‎

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

269+
def apply_aspect(self, position=None):
270+
if position is None:
271+
position = self.get_position(original=True)
272+
273+
# in the superclass, we would go through and actually deal with axis
274+
# scales and box/datalim. Those are all irrelevant - all we need to do
275+
# is make sure our coordinate system is square.
276+
figW, figH = self.get_figure().get_size_inches()
277+
fig_aspect = figH / figW
278+
box_aspect = 1
279+
pb = position.frozen()
280+
pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect)
281+
self.set_position(pb1.anchored(self.get_anchor(), pb), 'active')
282+
269283
@artist.allow_rasterization
270284
def draw(self, renderer):
271285
# 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
@@ -503,6 +503,13 @@ def test_axes3d_cla():
503503
ax.cla() # make sure the axis displayed is 3D (not 2D)
504504

505505

506+
@image_comparison(baseline_images=['axes3d_rotated'], extensions=['png'])
507+
def test_axes3d_rotated():
508+
fig = plt.figure()
509+
ax = fig.add_subplot(1, 1, 1, projection='3d')
510+
ax.view_init(90, 45) # look down, rotated. Should be square
511+
512+
506513
def test_plotsurface_1d_raises():
507514
x = np.linspace(0.5, 10, num=100)
508515
y = np.linspace(0.5, 10, num=100)

0 commit comments

Comments
 (0)