Python plt.axis('Equal')xlim

时间:2018-06-01 11:41:04

标签: python matplotlib axes

我有一个简单的3D曲面图,我希望轴在所有方向上相等。 我有以下代码:

tableView.layoutIfNeeded()

产生这个情节: enter image description here

然后我必须手动缩小以获得正确的轴限制。 我试过了import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm X = np.array([-100, 0, 100]) Y = np.array([ 0, 10, 20]) X_grid, Y_grid = np.meshgrid(X,Y) Z_grid = np.matrix('0 10 4;' '1 11 3;' '0 10 5') fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X_grid, Y_grid, Z_grid, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True) plt.axis('Equal') ,但它似乎没有回应? 此外,plt.xlim(-100,100)似乎不适用于z轴?

情节应如下所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以轻松地从评论中的链接调整策略,以便操作只影响X-Y平面:

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

X = np.array([-100,   0,  100])
Y = np.array([   0,  10,   20])

X_grid, Y_grid = np.meshgrid(X,Y)

Z_grid = np.matrix('0 10 4;'
                '1 11 3;'
                '0 10 5')

fig = plt.figure()
ax = fig.gca(projection='3d')

surf = ax.plot_surface(X_grid, Y_grid, Z_grid, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

max_range = np.array([X_grid.max()-X_grid.min(), Y_grid.max()-Y_grid.min()]).max() / 2.0

mid_x = (X_grid.max()+X_grid.min()) * 0.5
mid_y = (Y_grid.max()+Y_grid.min()) * 0.5

ax.set_xlim(mid_x - max_range, mid_x + max_range)
ax.set_ylim(mid_y - max_range, mid_y + max_range)

plt.show()

输出:

enter image description here