我正试图从500米乘40米的海底声纳数据中绘制出海底的三维图像。我将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 message from the mailing list中的示例脚本来构建和使用this branch of matplotlib,但在脚本中添加ax.pbaspect = [1.0, 1.0, 0.25]
行(卸载了matplotlib的“标准”版本以确保使用了自定义版本)并没有对生成的图像产生任何影响。
编辑:因此所需的输出类似于以下(用Inkscape粗略编辑)图像。在本例中,我没有在x/y轴上设置1:1的比率,因为它看起来非常薄,但是我已经展开它,所以它不像原始输出那样是正方形的。
我解决了浪费空间的问题:
对this question的回答对我来说非常有效。不需要设置任何比率,它会自动执行所有操作。
在savefig之前添加以下代码:
如果不需要方轴:
编辑网站包中的
get_proj
函数\mpl_toolkits\mplot3d\axes3d.py:然后添加一行设置pbaspect:
相关问题 更多 >
编程相关推荐