matplotlib Set_xlim和set_ylim不适用于3D打印中的轮廓

5ktev3wc  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(202)

我想创建一个二维切片轮廓图在三维与范围的x和y大于给定的xlim和ylim。然而,当我设置xlim和ylim时,轮廓似乎延伸到了轴之外。如果有一种方法可以限制轴内的轮廓,我将非常感激。

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

ax = plt.figure().add_subplot(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)

# Plot the 3D surface
#ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)

# Plot projections of the contours for each dimension.  By choosing offsets
# that match the appropriate axes limits, the projected contours will sit on
# the 'walls' of the graph
cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)

ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.set_zlim(-100, 100)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

图:

vlju58qv

vlju58qv1#

你可以用numpy.where过滤Z

Z = np.where((X > 20) | (X < -20), None, Z)
Z = np.where((Y > 20) | (Y < -20), None, Z)

示例:

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

ax = plt.figure().add_subplot(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)

Z = np.where((X > 20) | (X < -20), None, Z)
Z = np.where((Y > 20) | (Y < -20), None, Z)

# Plot the 3D surface
#ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)

# Plot projections of the contours for each dimension.  By choosing offsets
# that match the appropriate axes limits, the projected contours will sit on
# the 'walls' of the graph
cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)

ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.set_zlim(-100, 100)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

相关问题