使用下面的代码创建一条空间直线
import numpy as np import matplotlib.pyplot as plt # 创建一个空白画布 fig = plt.figure() # 创建一个子图 ax = fig.add_subplot(projection='3d') # 设置标签文本 ax.set_xlabel('X Axes') ax.set_ylabel('Y Axes') ax.set_zlabel('Z Axes') # 在三维坐标系中随便画一条直线 ax.plot3D(np.array([1, 10]), np.array([1, 10]), np.array([1, 10]), color='black') # 显示 plt.show()
效果如下图所示,创建三维图形时默认会显示灰色背景、网格线以及坐标轴。
可以加入 ax.grid(None) 指令将网格设为隐藏,加入 ax.axis('off') 指令将坐标轴设为隐藏
使用
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
可以将坐标轴的三个背景面设为白色。
加入上述指令后,代码和效果如下:
import numpy as np import matplotlib.pyplot as plt # 创建一个空白画布 fig = plt.figure() # 创建一个子图 ax = fig.add_subplot(projection='3d') # 设置网格不可见 ax.grid(None) # 设置X、Y、Z面的背景是白色 ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) # 设置坐标轴不可见 ax.axis('off') ax.set_xlabel('X Axes') ax.set_ylabel('Y Axes') ax.set_zlabel('Z Axes') # 在三维坐标系中随便画一条直线 ax.plot3D(np.array([1, 10]), np.array([1, 10]), np.array([1, 10]), color='black') plt.show()
运行代码后,可以看到空间三维直线的网格、坐标轴被隐藏,背景也变为白色。