设置matplotlib的三维图形比例?

2024-05-15 13:46:39 发布

您现在位置:Python中文网/ 问答频道 /正文

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

设置纵横比适用于二维打印:

ax = plt.axes()
ax.plot([0,1],[0,10])
ax.set_aspect('equal','box')

但不适用于3d:

ax = plt.axes(projection='3d')
ax.plot([0,1],[0,1],[0,10])
ax.set_aspect('equal','box')

3d案例是否有不同的语法,或者没有实现?


Tags: fromimportboxplotmatplotlibaspltequal
3条回答

我没有尝试所有这些答案,但这个笨蛋帮了我:

def axisEqual3D(ax):
    extents = np.array([getattr(ax, 'get_{}lim'.format(dim))() for dim in 'xyz'])
    sz = extents[:,1] - extents[:,0]
    centers = np.mean(extents, axis=1)
    maxsize = max(abs(sz))
    r = maxsize/2
    for ctr, dim in zip(centers, 'xyz'):
        getattr(ax, 'set_{}lim'.format(dim))(ctr - r, ctr + r)

我的理解是这基本上还没有实现。我也希望它能很快实施。有关可能的解决方案,请参见This link(我自己还没有测试过)。

看起来这个功能已经被添加了,所以我想我会为将来像我一样通过这个线程的人添加一个答案:

fig = plt.figure(figsize=plt.figaspect(0.5)*1.5) #Adjusts the aspect ratio and enlarges the figure (text does not enlarge)
ax = fig.gca(projection='3d')

figaspect(0.5)使图形的宽度是其高度的两倍。然后*1.5增加图形的大小。标签等不会增加,所以这是一种使图形看起来不那么杂乱无章的方法。

相关问题 更多 >