Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Ẩn trục 3D nhưng giữ nhãn trục trong Matplotlib

Để ẩn khung trục (axes panes/lines) nhưng giữ lại nhãn trục (axis labels) trong 3D plot, đặt màu pane và line thành trong suốt (alpha=0) và xóa ticks.

Các bước

  • Thiết lập figure size và autolayout.
  • Tạo figure và axes 3D.
  • Tạo dữ liệu bar3d.
  • Định nghĩa color_tuple = (1.0, 1.0, 1.0, 0.0) (trắng trong suốt).
  • Đặt pane color và line color cho x, y, z axes bằng color_tuple.
  • Xóa ticks: set_xticks([])...
  • Đặt nhãn trục: set_xlabel/ylabel/zlabel.
  • Hiển thị bằng show().

Ví dụ

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()

Kết quả

Ẩn trục 3D nhưng giữ nhãn trục trong Matplotlib