【发布时间】:2012-05-06 18:28:20
【问题描述】:
我正在尝试根据声纳在 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')
以及此脚本的输出图像:
现在我想更改它,以便沿轨道 (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 的比例,因为它看起来非常薄,但我已经将它展开,所以它不像原始输出那样是方形的。
【问题讨论】:
-
请参阅this question and answers 以获得解决方案。
标签: python matplotlib mplot3d