尽管设置了 alpha,但 3D 表面不透明 [英] 3D surface not transparent inspite of setting alpha
问题描述
我正在尝试创建具有透明度的3D表面.当我尝试下面的代码时,我希望得到一个立方体的两个半透明面.然而,尽管提供了 alpha=0.5 参数,但两个面都是不透明的.关于为什么发生这种情况以及如何解决的任何指示?我使用的是 Python 3.3(带有 QT 后端的 IPython 笔记本)和 Matplotlib 1.3.1.
I am trying to create a 3D surface with transparency. When I try the following code below, I expect to get two semi-transparent faces of a cube. However, both the faces are opaque inspite of supplying the alpha=0.5 argument. Any pointer on why this is happening and how to fix it ? I am using Python 3.3 (IPython notebook with the QT backend)and 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 元组来获得透明度.
Based on David Zwicker's input, I was able to get transparency working by setting the facecolor directly as a 4-tuple with alpha.
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)
这篇关于尽管设置了 alpha,但 3D 表面不透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!