gpt4 book ai didi

python - 3D 绘图纵横比 [matplotlib]

转载 作者:行者123 更新时间:2023-12-04 20:34:40 24 4
gpt4 key购买 nike

我正在使用编写的代码 here制作一个像下面这样的情节。

问题是,我想调整纵横比,即沿 z 轴拉伸(stretch)它,以便所有堆叠的图像或多或少可见。有没有一种简单的方法可以做到这一点?

enter image description here

最佳答案

看起来没有“正确”的方法可以做到这一点,但我们可以尝试通过猴子修补解决问题。这是我能做的最好的事情:

from mpl_toolkits.mplot3d import proj3d

def make_get_proj(self, rx, ry, rz):
'''
Return a variation on :func:`~mpl_toolkit.mplot2d.axes3d.Axes3D.getproj` that
makes the box aspect ratio equal to *rx:ry:rz*, using an axes object *self*.
'''

rm = max(rx, ry, rz)
kx = rm / rx; ky = rm / ry; kz = rm / rz;

# Copied directly from mpl_toolkit/mplot3d/axes3d.py. New or modified lines are
# marked by ##
def get_proj():
relev, razim = np.pi * self.elev/180, np.pi * self.azim/180

xmin, xmax = self.get_xlim3d()
ymin, ymax = self.get_ylim3d()
zmin, zmax = self.get_zlim3d()

# transform to uniform world coordinates 0-1.0,0-1.0,0-1.0
worldM = proj3d.world_transformation(xmin, xmax,
ymin, ymax,
zmin, zmax)

# adjust the aspect ratio ##
aspectM = proj3d.world_transformation(-kx + 1, kx, ##
-ky + 1, ky, ##
-kz + 1, kz) ##

# look into the middle of the new coordinates
R = np.array([0.5, 0.5, 0.5])

xp = R[0] + np.cos(razim) * np.cos(relev) * self.dist
yp = R[1] + np.sin(razim) * np.cos(relev) * self.dist
zp = R[2] + np.sin(relev) * self.dist
E = np.array((xp, yp, zp))

self.eye = E
self.vvec = R - E
self.vvec = self.vvec / proj3d.mod(self.vvec)

if abs(relev) > np.pi/2:
# upside down
V = np.array((0, 0, -1))
else:
V = np.array((0, 0, 1))
zfront, zback = -self.dist, self.dist

viewM = proj3d.view_transformation(E, R, V)
perspM = proj3d.persp_transformation(zfront, zback)
M0 = np.dot(viewM, np.dot(aspectM, worldM)) ##
M = np.dot(perspM, M0)
return M
return get_proj

# and later in the code:
ax.get_proj = make_get_proj(ax, 1, 1, 2)
ax.set_aspect(1.0)

3D plot inside a 1:1:2 box rendered by Matplotlib

关于python - 3D 绘图纵横比 [matplotlib],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com