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

Skip to content

Commit 4479a0f

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 081a639 commit 4479a0f

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
``Axes3D`` no longer distorts the 3d plot to match the 2d aspect ratio
2+
----------------------------------------------------------------------
3+
4+
Plots made with :class:`~mpl_toolkits.mplot3d.axes3d.Axes3D` were previously
5+
stretched to fit a square bounding box. As this stretching was done after
6+
the projection from 3D to 2D, it resulted in distorted images if non-square
7+
bounding boxes were used.
8+
9+
As of this release, this no longer occurs.

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,9 +1499,9 @@ def apply_aspect(self, position=None):
14991499
"""
15001500
Adjust the Axes for a specified data aspect ratio.
15011501
1502-
Depending on `.get_adjustable` this will modify either the Axes box
1503-
(position) or the view limits. In the former case, `.get_anchor`
1504-
will affect the position.
1502+
Depending on `.get_adjustable` this will modify either the
1503+
Axes box (position) or the view limits. In the former case,
1504+
`~matplotlib.axes.Axes.get_anchor` will affect the position.
15051505
15061506
Notes
15071507
-----

lib/mpl_toolkits/mplot3d/axes3d.py

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

258+
def apply_aspect(self, position=None):
259+
if position is None:
260+
position = self.get_position(original=True)
261+
262+
# in the superclass, we would go through and actually deal with axis
263+
# scales and box/datalim. Those are all irrelevant - all we need to do
264+
# is make sure our coordinate system is square.
265+
figW, figH = self.get_figure().get_size_inches()
266+
fig_aspect = figH / figW
267+
box_aspect = 1
268+
pb = position.frozen()
269+
pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect)
270+
self.set_position(pb1.anchored(self.get_anchor(), pb), 'active')
271+
258272
@artist.allow_rasterization
259273
def draw(self, renderer):
260274
# 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
@@ -511,6 +511,13 @@ def test_axes3d_cla():
511511
ax.cla() # make sure the axis displayed is 3D (not 2D)
512512

513513

514+
@image_comparison(['axes3d_rotated.png'])
515+
def test_axes3d_rotated():
516+
fig = plt.figure()
517+
ax = fig.add_subplot(1, 1, 1, projection='3d')
518+
ax.view_init(90, 45) # look down, rotated. Should be square
519+
520+
514521
def test_plotsurface_1d_raises():
515522
x = np.linspace(0.5, 10, num=100)
516523
y = np.linspace(0.5, 10, num=100)

0 commit comments

Comments
 (0)