Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ทำให้ Poly3DCollection โปร่งใสใน Matplotlib 3D

ใช้ Poly3DCollection กับพารามิเตอร์ alpha (0-1) สำหรับความโปร่งใสของหน้า 3D

ตัวอย่างโค้ด


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

ผลลัพธ์

ทรง 3D (tetrahedron) โปร่งใส 50% พร้อมเส้นขอบประ

ทำให้ Poly3DCollection โปร่งใสใน Matplotlib 3D