Để vẽ đồ thị Poly3DCollection trong suốt trong Matplotlib, ta thực hiện các bước:
- Thiết lập kích thước hình và điều chỉnh khoảng đệm giữa và xung quanh subplots.
- Tạo hình mới hoặc kích hoạt hình hiện có.
- Thêm '~.axes.Axes' vào hình như phần của subplot với projection=3d.
- Tạo điểm dữ liệu x, y và z.
- Tạo danh sách đỉnh (vertices).
- Chuyển x, y, z thành danh sách zip tuple.
- Lấy danh sách instance Poly3d.
- Thêm đối tượng collection 3D vào đồ thị dùng add_collection3d().
- Tắt trục tọa độ.
- Hiển thị hình dùng show().
Ví dụ
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [0, 2, 1, 1]
y = [0, 0, 1, 0]
z = [0, 0, 0, 1]
vertices = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]]
tupleList = list(zip(x, y, z))
poly3d = [[tupleList[vertices[ix][iy]]
for iy in range(len(vertices[0]))]
for ix in range(len(vertices))]
ax.scatter(x, y, z)
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.5))
ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=2, linestyles='--'))
plt.axis('off')
plt.show()
Kết quả
