marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.04k stars 265 forks source link

Creating manual legend entries in a plotter #829

Open mathewwe opened 1 year ago

mathewwe commented 1 year ago

Hi @marcomusy ,

I am working on coloring certain sections of polygons in a mesh, which has been working well. However, I would also like to use a LegendBox or something similar to label the different colored sections of the mesh. As of right now, I have not found a way to do this. It looks like the LegendBox class only accepts separate objects as entries. Is it possible to manually add entries? Otherwise, should the mesh be split into parts, or a dummy object added to create a legend entry manually? Any advice would be helpful. For reference, the relevant code snippet for testing the mesh colorization is below:

mesh = Mesh(file_in)
nv = mesh.ncells
scals = range(nv)
lut = build_lut([
       (7200, 'green', 1.0),
       (14454, 'red'  , 1.0),
      ],
      vmin=0, vmax=nv,
     )
mesh.cmap(lut, scals, on='cells')

self.plt = vedo.Plotter(qt_widget=self.vedo_widget, interactive=False)
self.plt.show(mesh.compute_normals().phong())

Thanks!

marcomusy commented 1 year ago

Hi, yes that could be a nice feature to add, meanwhile you can create a dummy Marker object e.g:

from vedo import *

mesh = Mesh(dataurl + "bunny.obj")

nv = mesh.ncells
scals = range(nv)
lut = build_lut(
    [
        (nv / 2, "green5"),
        (nv, "red5"),
    ],
    vmin=0,
    vmax=nv,
)
mesh.cmap(lut, scals, on="cells")
mesh.legend("Bunny mesh")

mk1 = Marker("*").c("green5").legend("cool faces")
mk2 = Marker("*").c("red5").legend("ugly faces")
lb = LegendBox(
    [mesh, mk1, mk2],
    markers=["s", "o", "0"],
    font="Cartoons123",
    width=0.4,
    height=0.2,
)

plt = Plotter()
plt.show(mesh, lb, axes=1)
Screenshot 2023-03-08 at 23 16 08