Set aspect ratio of matplotlib 3D graphics?

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

The aspect ratio setting works for 2d graphs:

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

But not for 3d:

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

Is there any other syntax for 3d code or is it not implemented?

+27
source share
5 answers

My understanding basically lies in the fact that this is not yet implemented. I also hope that this will be implemented soon. See this link for a possible solution (I have not tested it myself).

+4
source

I have not tried all of these answers, but this kludge did this for me:

def axisEqual3D(ax):
    extents = np.array([getattr(ax, 'get_{}lim'.format(dim))() for dim in 'xyz'])
    sz = extents[:,1] - extents[:,0]
    centers = np.mean(extents, axis=1)
    maxsize = max(abs(sz))
    r = maxsize/2
    for ctr, dim in zip(centers, 'xyz'):
        getattr(ax, 'set_{}lim'.format(dim))(ctr - r, ctr + r)
+19
source

, , , , , :

fig = plt.figure(figsize=plt.figaspect(0.5)*1.5) #Adjusts the aspect ratio and enlarges the figure (text does not enlarge)
ax = fig.gca(projection='3d')

figaspect(0.5) , . *1.5 . .. , .

+13

, . + -3 (0,0,0), :

import numpy as np
import pylab as pl
from mpl_toolkits.mplot3d import Axes3D
fig = pl.figure()
ax = fig.gca(projection='3d')
ax.set_aspect('equal')
MAX = 3
for direction in (-1, 1):
    for point in np.diag(direction * MAX * np.array([1,1,1])):
        ax.plot([point[0]], [point[1]], [point[2]], 'w')
+10

, :

ax.auto_scale_xyz([minbound, maxbound], [minbound, maxbound], [minbound, maxbound])
+6

Source: https://habr.com/ru/post/1599053/


All Articles