python - 尽管设置了 alpha,3D 表面仍不透明

标签 python matplotlib

我正在尝试创建一个具有透明度的 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)

Transparency by setting face color directly

关于python - 尽管设置了 alpha,3D 表面仍不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403293/

相关文章:

python - 抑制 argparse 错误消息 - Python

python - 在给定 XY 边界之间的 Matplotlib 图中显示图像

python - Matplotlib 显示页面上的滚动条

python - 在同一个窗口中绘制多种类型的图(折线图、散点图、条形图等)

python - 使用 PyQt5 和 Matplotlib 制作动态图

python - 使用 list(filter()) 的程序在 python 3 上运行速度极慢

python - 仅当有一个行具有空值时,才向前填充数据框中的一列

Python IP地址发现代码给出外部IP而不是eth0设备IP

python - 如何使用异步遍历列表并调用列表对象自己的函数

python - 如何将仅包含字符串的表添加到 matplotlib 图形中