matplotlib(单位长度相等):纵横比“相等"时,z轴不等于x-和y- [英] matplotlib (equal unit length): with 'equal' aspect ratio z-axis is not equal to x- and y-
问题描述
当我为3d图形设置相等的长宽比时,z轴不会变为相等".所以,这个:
When I set up equal aspect ratio for 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 could find did not work. Thank you.
推荐答案
我相信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.gca(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-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!