设置 3D 绘图的纵横比 [英] Setting aspect ratio of 3D plot

查看:37
本文介绍了设置 3D 绘图的纵横比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to plot a 3D image of the seafloor from the data of a sonar run over a 500m by 40m portion of the seafloor. I am using matplotlib/mplot3d with Axes3D and I want to be able to change the aspect ratio of the axes so that the x & y axis are to scale. An example script with generated data rather than the real data is:

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')

以及这个脚本的输出图像:

And the output image from this script:

现在我想改变它,使沿轨道 (x) 轴上的 1 米与范围 (y) 轴上的 1 米相同(或者可能是不同的比率,具体取决于所涉及的相对尺寸).我还想设置 z 轴的比例,由于数据中的相对大小,同样不必设置为 1:1,但轴比当前图小.

Now I would like to change it so that 1 metre in the along-track (x) axis is the same as 1 metre in the range (y) axis (or maybe a different ratio depending on the relative sizes involved). I would also like to set the ratio of the z-axis, again not neccessarily to 1:1 due to the relative sizes in the data, but so the axis is smaller than the current plot.

我尝试构建和使用 matplotlib 的这个分支,按照示例邮件列表中的此邮件中的脚本,但添加了 ax.pbaspect = [1.0, 1.0, 0.25] 我的脚本行(已卸载 matplotlib 的标准"版本以确保使用自定义版本)对生成的图像没有任何影响.

I have tried building and using this branch of matplotlib, following the example script in this message from the mailing list, but adding the ax.pbaspect = [1.0, 1.0, 0.25] line to my script (having uninstalled the 'standard' version of matplotlib to ensure the custom version was being used) didn't make any difference in the generated image.

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

So the desired output would be something like the following (crudely edited with Inkscape) image. In this case I haven't set a 1:1 ratio on the x/y axes because that looks ridiculously thin, but I have spread it out so it isn't square as on the original output.

推荐答案

在savefig前添加如下代码:

Add following code before savefig:

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

如果你不想要方轴:

编辑 site-packagesmpl_toolkitsmplot3daxes3d.py 中的 get_proj 函数:

edit the get_proj function inside site-packagesmpl_toolkitsmplot3daxes3d.py:

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:

then add one line to set pbaspect:

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

这篇关于设置 3D 绘图的纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆