Matplotlip:3D曲面图关闭背景但保留轴

时间:2017-05-16 12:33:58

标签: python matplotlib plot

我想做一个显示轴的3D曲面图,但不显示轴之间的面。我发现如何使用ax.set_axis_off()关闭轴和面。有没有机会只关掉那些面孔,或者让它们变得透明? (在第一张图片中,如果仔细观察,可以看到面孔) With axes and background Without axes and background

提前致谢!

1 个答案:

答案 0 :(得分:13)

你无法“关闭窗格”,但你可以改变它们的颜色,从而使它们透明。

ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))

完整代码:

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

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlabel("x"); ax.set_ylabel("y"); ax.set_zlabel("z")

x = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(x,x)
Z = np.sin(np.sqrt(X**2 + Y**2))

# make the panes transparent
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# make the grid lines transparent
ax.xaxis._axinfo["grid"]['color'] =  (1,1,1,0)
ax.yaxis._axinfo["grid"]['color'] =  (1,1,1,0)
ax.zaxis._axinfo["grid"]['color'] =  (1,1,1,0)

surf = ax.plot_surface(X, Y, Z, cmap=plt.cm.coolwarm,
                   linewidth=0, antialiased=False)

fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

enter image description here