gpt4 book ai didi

python - matplotlib 中 Poly3DCollection 图的透明度

转载 作者:太空狗 更新时间:2023-10-29 17:55:29 30 4
gpt4 key购买 nike

我正在尝试使用用于 Python 的出色的 Matplotlib 包绘制一些对象。这些对象由使用 plt.scatter() 实现的点和使用 Poly3DCollection 实现的面片组成。我希望补丁具有轻微的透明度,以便可以看到补丁后面的点和边缘。

这里是我已经生成的代码和绘图。似乎我快到了,只是缺少透明度的功能。有趣的是,如果我先绘制 Ploy3DCollection,然后绘制 scatter 点,则可以看到这些点,但看不到边缘。

有人对我有什么建议吗?

enter image description here

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

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

plt.show()

最佳答案

我对 OP 代码稍作修改,使透明度正常工作。 Poly3DCollection 的 facecolors 参数似乎覆盖了透明度参数,因此解决方案是在单独调用 Poly3DCollection.set_colorPoly3DCollection.set_facecolor 中设置颜色:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

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 = 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)
collection = Poly3DCollection(poly3d, linewidths=1, alpha=0.2)
face_color = [0.5, 0.5, 1] # alternative: matplotlib.colors.rgb2hex([0.5, 0.5, 1])
collection.set_facecolor(face_color)
ax.add_collection3d(collection)

plt.show()

有趣的是,如果您使用 collection.set_edgecolor('k') 显式设置边缘颜色,边缘也会遵循透明度设置。

关于python - matplotlib 中 Poly3DCollection 图的透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18897786/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com