【问题标题】:3D surface not transparent inspite of setting alpha尽管设置了 alpha,但 3D 表面不透明
【发布时间】:2014-06-17 16:06:42
【问题描述】:

我正在尝试创建具有透明度的 3D 表面。当我尝试下面的代码时,我希望得到一个立方体的两个半透明面。然而,尽管提供了 alpha=0.5 参数,但两个面都是不透明的。关于为什么会发生这种情况以及如何解决它的任何指针?我正在使用 Python 3.3(带有 QT 后端的 IPython notebook)和 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)

【问题讨论】:

标签: python matplotlib


【解决方案1】:

根据 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)

【讨论】:

  • alpha 值实际上对您有什么帮助吗?如您的回答所示,我确实获得了透明度,但无论alpha如何,它都保持在同一水平
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 2011-07-02
  • 2012-11-24
  • 2017-09-29
  • 2016-01-31
相关资源
最近更新 更多