我正在尝试创建一个具有透明度的 3D 表面。当我尝试下面的代码时,我希望得到立方体的两个半透明面。然而,尽管提供了 alpha=0.5 参数,但两个面都是不透明的。关于为什么会发生这种情况以及如何解决它的任何指示?我正在使用 Python 3.3(带有 QT 后端的 IPython 笔记本)和 Matplotlib 1.3.1。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d as mp3d
bot = [(0, 0, 0),
(1, 0, 0),
(1, 1, 0),
(0, 1, 0),
]
top = [(0, 0, 1),
(1, 0, 1),
(1, 1, 1),
(0, 1, 1),
]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
face1 = mp3d.art3d.Poly3DCollection([bot], alpha=0.5, linewidth=1)
face2 = mp3d.art3d.Poly3DCollection([top], alpha=0.5, linewidth=1)
ax.add_collection3d(face1)
ax.add_collection3d(face2)
最佳答案
根据 David Zwicker 的意见,我能够通过将 facecolor 直接设置为带 alpha 的 4 元组来获得透明度。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d as mp3d
bot = [(0, 0, 0),
(1, 0, 0),
(1, 1, 0),
(0, 1, 0),
]
top = [(0, 0, 1),
(1, 0, 1),
(1, 1, 1),
(0, 1, 1),
]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
face1 = mp3d.art3d.Poly3DCollection([bot], alpha=0.5, linewidth=1)
face2 = mp3d.art3d.Poly3DCollection([top], alpha=0.5, linewidth=1)
# This is the key step to get transparency working
alpha = 0.5
face1.set_facecolor((0, 0, 1, alpha))
face2.set_facecolor((0, 0, 1, alpha))
ax.add_collection3d(face1)
ax.add_collection3d(face2)
关于python - 尽管设置了 alpha,3D 表面仍不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403293/