本文介绍了matplotlib(相等的单位长度):具有“相等"纵横比的 z 轴不等于 x- 和 y-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为 3d 图形设置相等的纵横比时,z 轴 不会更改为相等".所以这个:

When I set up an equal aspect ratio for a 3d graph, the z-axis does not change to 'equal'. So this:

fig = pylab.figure()
mesFig = fig.gca(projection='3d', adjustable='box')
mesFig.axis('equal')
mesFig.plot(xC, yC, zC, 'r.')
mesFig.plot(xO, yO, zO, 'b.')
pyplot.show()

给我以下内容:

显然 z 轴的单位长度不等于 x 和 y 单位.

Where obviously the unit length of z-axis is not equal to x- and y- units.

如何使三个轴的单位长度相等?我找到的所有解决方案都不起作用.

How can I make the unit length of all three axes equal? All the solutions I found did not work.

推荐答案

我相信 matplotlib 还没有在 3D 中正确设置相等的轴......但我前段时间发现了一个技巧(我不记得在哪里)我已经适应使用它.这个概念是在你的数据周围创建一个假的三次边界框.您可以使用以下代码对其进行测试:

I believe matplotlib does not yet set correctly equal axis in 3D... But I found a trick some times ago (I don't remember where) that I've adapted using it. The concept is to create a fake cubic bounding box around your data.You can test it with the following code:

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

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.set_aspect('equal')

X = np.random.rand(100)*10+5
Y = np.random.rand(100)*5+2.5
Z = np.random.rand(100)*50+25

scat = ax.scatter(X, Y, Z)

# Create cubic bounding box to simulate equal aspect ratio
max_range = np.array([X.max()-X.min(), Y.max()-Y.min(), Z.max()-Z.min()]).max()
Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(X.max()+X.min())
Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(Y.max()+Y.min())
Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(Z.max()+Z.min())
# Comment or uncomment following both lines to test the fake bounding box:
for xb, yb, zb in zip(Xb, Yb, Zb):
   ax.plot([xb], [yb], [zb], 'w')

plt.grid()
plt.show()

z 数据大约比 x 和 y 大一个数量级,但即使使用等轴选项,matplotlib 也会自动缩放 z 轴:

z data are about an order of magnitude larger than x and y, but even with equal axis option, matplotlib autoscale z axis:

但是如果添加边界框,则会获得正确的缩放:

But if you add the bounding box, you obtain a correct scaling:

这篇关于matplotlib(相等的单位长度):具有“相等"纵横比的 z 轴不等于 x- 和 y-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 20:57