【问题标题】:How to color individual elements in matplotlib plot_trisurf如何在 matplotlib plot_trisurf 中为单个元素着色
【发布时间】:2021-04-07 10:31:55
【问题描述】:

我有一个三角形表面网格,通过移动立方体生成。我想根据绘图中的任意变量为表面元素着色。我目前正在使用 plot_trisurf,但是当我阅读文档时,看起来颜色总是基于 Z 分量,或者是一个常数?我可以不为每个元素指定颜色吗?还是有更好的情节例程可以使用?例如,下面的代码行是我所追求的,其中颜色基于行进立方体的“值”输出。谢谢!

import numpy as np
import matplotlib.pyplot as plt

from skimage import measure
from skimage.draw import ellipsoid

from matplotlib import cm


# Generate a level set about zero of two identical ellipsoids in 3D
ellip_base = ellipsoid(6, 10, 16, levelset=True)
# Use marching cubes to obtain the surface mesh of these ellipsoids
verts, faces, normals, values = measure.marching_cubes_lewiner(ellip_double, 0)

fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(111, projection='3d')

ax.plot_trisurf(verts[:, 0], verts[:,1], verts[:, 2], triangles=faces, cmap=cm.rainbow, color=values)

ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

ax.set_xlim(0, 24)  
ax.set_ylim(0, 20) 
ax.set_zlim(0, 32)  

plt.tight_layout()
plt.show()

【问题讨论】:

  • 也许plot_trisurface with custom color array 与此相关?这似乎暗示了ax.plot_trisurf(...., facecolors=cmap(norm(values)))。另请注意,matplotlib 仅具有有限的 3D 功能。你可能想看看例如玛雅维。
  • 在那里挖掘,他们没有在命令中指定颜色,但您显然可以在事后设置它。我会发布作为答案,谢谢!

标签: python matplotlib


【解决方案1】:

正如 JohanC 所指出的,在this question 中,他们在事后重置了面部颜色。这对我有用,因为我已经定义了一组颜色。

# set the face colors of the Poly3DCollection
p3dc.set_fc(colors)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 2018-02-28
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    相关资源
    最近更新 更多