首页 > 解决方案 > How to do a 3D plot with equal aspect ratio adjustable with datalim?

问题描述

I am plotting a set of Line3d objects in my axes3D. I want to keep all the axes with equal scale and eliminate the margins between the axes and the figure. And this needs to hold for any shape of the figure.

So far i have tried this:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_proj_type('ortho')
ax.set_aspect('equal', adjustable='datalim', anchor='C')
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
ax.set_axis_on()

ax.plot3D([0,0], [0,0], [0,1], 'blue', marker='s', markersize=5)
ax.plot3D([0,1], [0,0], [1,1], 'blue', marker='s', markersize=5)
ax.plot3D([1,1], [0,1], [1,1], 'blue', marker='s', markersize=5)

plt.show()

That gives the following figure that clearly does not maintain the scale in each axis: enter image description here

I also tried by changing:

ax.set_aspect('equal', adjustable='box', anchor='C')

It gives the following figure, that does keep the axis with equal scaled: enter image description here

But this is not what i want since the axes do not cover the entire figure box, as can be seen when I zoom in the axes: enter image description here

So basically i want something similar to the first figure, but with the axis limits changed so to maintain the axis aspect ratio, even when zooming in and out.

I have tried other alternatives such as axis('scaled') without success. I know that for 2d plotting this works fine just by setting ax.set_aspect('equal') (see link here),but it doesn't seem to be the case for 3D axes.

标签: pythonmatplotlibmplot3d

解决方案


推荐阅读