3D图长宽比[matplotlib]

Hay*_*yan 5 python plot matplotlib

我正在使用这里编写的代码下面绘制这样的图。

问题是,我想调整宽高比,即沿z轴拉伸,以便使所有堆叠的图像或多或少可见。有一个简单的方法吗?

在此处输入图片说明

Ale*_*kin 4

好像没有 xe2\x80\x9cproper\xe2\x80\x9d 的方法来做到这一点,但我们可以尝试猴子修补我们的方法来解决这个问题。这里\xe2\x80\x99s是我能做的最好的组装:

\n\n
from mpl_toolkits.mplot3d import proj3d\n\ndef make_get_proj(self, rx, ry, rz):\n    \'\'\'\n    Return a variation on :func:`~mpl_toolkit.mplot2d.axes3d.Axes3D.getproj` that\n    makes the box aspect ratio equal to *rx:ry:rz*, using an axes object *self*.\n    \'\'\'\n\n    rm = max(rx, ry, rz)\n    kx = rm / rx; ky = rm / ry; kz = rm / rz;\n\n    # Copied directly from mpl_toolkit/mplot3d/axes3d.py. New or modified lines are\n    # marked by ##\n    def get_proj():\n        relev, razim = np.pi * self.elev/180, np.pi * self.azim/180\n\n        xmin, xmax = self.get_xlim3d()\n        ymin, ymax = self.get_ylim3d()\n        zmin, zmax = self.get_zlim3d()\n\n        # transform to uniform world coordinates 0-1.0,0-1.0,0-1.0\n        worldM = proj3d.world_transformation(xmin, xmax,\n                                             ymin, ymax,\n                                             zmin, zmax)\n\n        # adjust the aspect ratio                          ##\n        aspectM = proj3d.world_transformation(-kx + 1, kx, ##\n                                              -ky + 1, ky, ##\n                                              -kz + 1, kz) ##\n\n        # look into the middle of the new coordinates\n        R = np.array([0.5, 0.5, 0.5])\n\n        xp = R[0] + np.cos(razim) * np.cos(relev) * self.dist\n        yp = R[1] + np.sin(razim) * np.cos(relev) * self.dist\n        zp = R[2] + np.sin(relev) * self.dist\n        E = np.array((xp, yp, zp))\n\n        self.eye = E\n        self.vvec = R - E\n        self.vvec = self.vvec / proj3d.mod(self.vvec)\n\n        if abs(relev) > np.pi/2:\n            # upside down\n            V = np.array((0, 0, -1))\n        else:\n            V = np.array((0, 0, 1))\n        zfront, zback = -self.dist, self.dist\n\n        viewM = proj3d.view_transformation(E, R, V)\n        perspM = proj3d.persp_transformation(zfront, zback)\n        M0 = np.dot(viewM, np.dot(aspectM, worldM)) ##\n        M = np.dot(perspM, M0)\n        return M\n    return get_proj\n\n# and later in the code:\nax.get_proj = make_get_proj(ax, 1, 1, 2)\nax.set_aspect(1.0)\n
Run Code Online (Sandbox Code Playgroud)\n\n

由 Matplotlib 渲染的 1:1:2 盒子内的 3D 绘图

\n