如何使用 Matplotlib 在 3D 图中隐藏轴但保留轴标签?
matplotlibpythondata visualization更新于 2025/6/3 7:08:17
要使用 Matplotlib 在 3D 图中隐藏轴但保留轴标签,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 创建新图形或激活现有图形。
- 添加 '~.axes.Axes'作为子图排列的一部分添加到图形中。
- 使用 numpy 创建 x、y、z、dx、dy 和 dz 数据点
- 使用 bar3d() 方法绘制 3D 条形图。
- 要隐藏轴,请初始化颜色元组,与轴颜色相同。
- 将 x、y 和 z 轴平面颜色属性设置为与颜色元组相同。
- 将 x、y 和 z 轴线颜色属性设置为与颜色元组相同。
- 将空刻度设置为 x、y 和 z 轴。
- 设置 x、y 和 z 轴标签。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [5, 6, 7, 8, 2, 5, 6, 3, 7, 2]
z = np.zeros(10)
dx = np.ones(10)
dy = np.ones(10)
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ax.bar3d(x, y, z, dx, dy, dz, color="green")
color_tuple = (1.0, 1.0, 1.0, 0.0)
ax.w_xaxis.set_pane_color(color_tuple)
ax.w_yaxis.set_pane_color(color_tuple)
ax.w_zaxis.set_pane_color(color_tuple)
ax.w_xaxis.line.set_color(color_tuple)
ax.w_yaxis.line.set_color(color_tuple)
ax.w_zaxis.line.set_color(color_tuple)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_zlabel('Z-Axis')
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

