gpt4 book ai didi

python - 设置 3D 绘图的纵横比

转载 作者:IT老高 更新时间:2023-10-28 22:12:33 26 4
gpt4 key购买 nike

我正在尝试根据声纳在 500m x 40m 的海底部分上运行的数据绘制海底的 3D 图像。我将 matplotlib/mplot3d 与 Axes3D 一起使用,我希望能够更改轴的纵横比,以便 x 和 y 轴按比例缩放。包含生成数据而不是真实数据的示例脚本是:

import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Create figure.
fig = plt.figure()
ax = fig.gca(projection = '3d')

# Generate example data.
R, Y = np.meshgrid(np.arange(0, 500, 0.5), np.arange(0, 40, 0.5))
z = 0.1 * np.abs(np.sin(R/40) * np.sin(Y/6))

# Plot the data.
surf = ax.plot_surface(R, Y, z, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)

# Set viewpoint.
ax.azim = -160
ax.elev = 30

# Label axes.
ax.set_xlabel('Along track (m)')
ax.set_ylabel('Range (m)')
ax.set_zlabel('Height (m)')

# Save image.
fig.savefig('data.png')

以及此脚本的输出图像:

matplotlib output image

现在我想更改它,以便沿轨道 (x) 轴上的 1 米与范围 (y) 轴上的 1 米相同(或者可能是不同的比率,具体取决于所涉及的相对尺寸)。我还想设置 z 轴的比率,由于数据中的相对大小,再次不一定要设置为 1:1,但是轴比当前绘图要小。

我尝试过构建和使用 this branch of matplotlib ,按照 this message from the mailing list 中的示例脚本,但是将 ax.pbaspect = [1.0, 1.0, 0.25] 行添加到我的脚本(已卸载 matplotlib 的“标准”版本以确保正在使用自定义版本)并没有产生任何影响生成图像的差异。

编辑:因此所需的输出将类似于以下(使用 Inkscape 粗略编辑)图像。在这种情况下,我没有在 x/y 轴上设置 1:1 的比例,因为它看起来非常薄,但我已经将它展开,所以它不像原始输出那样是方形的。

Desired output

最佳答案

在savefig前添加如下代码:

ax.auto_scale_xyz([0, 500], [0, 500], [0, 0.15])

enter image description here

如果你不想要方轴:

编辑 site-packages\mpl_toolkits\mplot3d\axes3d.py 中的 get_proj 函数:

xmin, xmax = np.divide(self.get_xlim3d(), self.pbaspect[0])
ymin, ymax = np.divide(self.get_ylim3d(), self.pbaspect[1])
zmin, zmax = np.divide(self.get_zlim3d(), self.pbaspect[2])

然后添加一行设置pbaspect:

ax = fig.gca(projection = '3d')
ax.pbaspect = [2.0, 0.6, 0.25]

enter image description here

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

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