如何在使用 Matplotlib 3D 繪圖時隱藏座標軸但保留座標軸標籤
如果想使用 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()
輸出
廣告