pythonmatplotlibprojectionmatplotlib-3d

Way to specify viewpoint distance in 3d plots


I am working on an animation of a 3D plot using mpl_toolkits.mplot3d (Matplotlib 3.6.3) and need to set the view distance.

It seems that earlier versions of Matplotlib allowed the elevation, azimuth, and distance of the viewpoint "camera" to be set for 3D plots using methods like this:

ax.elev = 45
ax.azim = 10
ax.dist = 2

but the distance attribute appears to have been deprecated for some reason:

Warning (from warnings module):
    ax.dist = 2
MatplotlibDeprecationWarning: The dist attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.

This still runs, but the output plots have all sorts of visual artifacts that only go away if I shut off the axes with ax.set_axis_off().

Is there an equivalent means of setting the viewpoint distance to zoom in on a 3D data set in 3.6.3?


Solution

  • According to the documentation, you now need to use the zoom argument of set_box_aspect:

    ax.set_box_aspect(None, zoom=2)

    Where the first argument is the aspect ratio. Use None to use the default values.