Search code examples
pythonmatplotlibplotsurface

how to set the scale of Z axis equal to X and Y axises in python plot_surface


I tried use plot_surface in matplotlib to plot a 3D surface. My code is:

fig = plt.figure()
ax = fig.gca(projection='3d')
# ax.set_aspect('equal')
ax.plot_surface(X, Y, H4200, rstride=1000, cstride=1000)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# ax._autoscaleXon = True
# ax._autoscaleYon = True
# ax._autoscaleZon = True
# ax.set_aspect('equal', adjustable='box')

plt.show()

the result of the plot are:

1:

enter image description here

2:

enter image description here

obviously, the length of Z units is not equal to the units of X and Y axises.

My question is how to set the scale of Z axis equal to the scale of X and Y axises?

I tried ax.set_aspect('equal'), ax._autoscaleXon = True, ax._autoscaleYon = True, ax._autoscaleZon = True, ax.set_aspect('equal', adjustable='box') as seen in my comments, but it's not work, How can I do to resolve this question? I need your help, Thanks!


Solution

  • Try,

    ax.set_aspect(1)
    

    It works for me.

    Edit 1

    In your case, I suspect that set_aspect(1) or set_aspect('equal') fails because matplotlib forces autoscale z axis to accommodate the whole range of your z data (with a different order of magnitude than x and y). In such situation, you need some tricks that are available here.