Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set matplotlib 3d plot aspect ratio

import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D 

Setting the aspect ratio works for 2d plots:

ax = plt.axes() ax.plot([0,1],[0,10]) ax.set_aspect('equal','box') 

But does not for 3d:

ax = plt.axes(projection='3d') ax.plot([0,1],[0,1],[0,10]) ax.set_aspect('equal','box') 

Is there a different syntax for the 3d case, or it's not implemented?

like image 656
hatmatrix Avatar asked Nov 15 '11 02:11

hatmatrix


People also ask

How do I zoom in Matplotlib 3D?

Matplotlib mplot3d toolkit One can rotate the 3D scene by simply clicking-and-dragging the scene. Zooming is done by right-clicking the scene and dragging the mouse up and down. Note that one does not use the zoom button like one would use for regular 2D plots.


1 Answers

As of matplotlib 3.3.0, Axes3D.set_box_aspect seems to be the recommended approach.

import numpy as np import matplotlib.pyplot as plt  xs, ys, zs = ... ax = plt.axes(projection='3d')  ax.set_box_aspect((np.ptp(xs), np.ptp(ys), np.ptp(zs)))  # aspect ratio is 1:1:1 in data space  ax.plot(xs, ys, zs) 
like image 50
Matt Panzer Avatar answered Sep 28 '22 08:09

Matt Panzer