Set color to a user-defined function for each cell of a 3D Delaunay triangulation

I’ve got a 3D surface built on Delaunay triangulation since my data are ungridded (or unstructured). But each individual triangle displays a uniform color. My data being rapidly changing, a single triangle may find itself going from a very low to a very high value: thus it is not fair to have it with a single color. Do you know how to enforce Plotly to paint each triangle not with a uniform color but with a user-defined-function dependent color ? (in this particular example, the function would be just z, z being the vertical coord.)

@ifffam After performing Delaunay triangulation of your surface, you should extract the lists of x, y, and z-coordinates of triangle vertices, and the lists I, J, K derived from Delaunay simplices. (a simplex (triangle) consists in three integers (i, j, k), representing each the index in the array of vertices of points that define that triangle.
If you have n triangles then you should set a rule of assigning a color to each triangle.
I triangulated the Mobius strip, with 322 triangles (simplices).

import plotly.graph_objs as go
import numpy as np
from scipy.spatial import Delaunay

u = np.linspace(0,2*np.pi, 24)
v = np.linspace(-1,1, 8)
u, v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()
#evaluate the parameterization at the flattened u and v
tp = 1+0.5*v*np.cos(u/2)
x = tp*np.cos(u)
y = tp*np.sin(u)
z = 0.5*v*np.sin(u/2)
points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
I, J, K = tri.simplices.T
facecolor=32*['red', 'green', 'blue', 'orange', 'magenta', 'brown', 'yellow', 'pink', 'white', 'gray']
facecolor.extend(['red', 'green'])

Now plot the triangulated surface via go.Mesh3d:

fig=go.Figure(go.Mesh3d(x=x, y=y, z=z, i=I, j=J, k=K, facecolor=facecolor))
fig.update_layout(width=650, height=500, font_size=10, 
                  scene_camera_eye=dict(x=1.5, y=1.5, z=0.95))

and this is the surface:

@empet I appreciate your input. The goal was not to assign a color to each simplice according to some more or less esoteric function, but to allow each simplice to have a blend of colors in terms of a function, or just for simplicity, in terms of the z coordinate, which means that horizontal simplices will have a constant color, like all simplices of your plot, and each of the vertical simplices will have a bunch of colors, from the ones that you have defined (from red to grey, or from some other palette of your choice).

To color each triangle according to values of a function ( for example z) set in the
go. Mesh3d definition intensity=z, and colorscale=some_plotly_colorscale.Example here:
For other posibilities inspect all mesh attributes via

See also `intensitymode=‘celll’, to color each cell according to a function value associated to that cell:

I’ve done as you said and all I get is a number of arrays displayed, but not the plot itself… sorry for my ignorance, but seeing that, I added as is normally done in matplotlib… but it did not work here… so plotting via g0.Mesh3d seems to be a completely different thing… what should I add to plot the figure ? (from your first url (15522), I don’t see clearly what I should do)

Both notebooks have been created 4 years ago or more and at that time there was a different way to display a fig, via iplot.
With the actual plotly version `’ displays it. Don’ t use matplotlib tips here because they do not work for plotly. You addressed another question on this forum illustrated with a matplotlib code and did not get an answer because the question is not clear in the plotly context.

I’m trying to run your script of the Moebius strip from

When I get to a given line, it gives me an error message:

… data1 = plotly_triangular_mesh(x, y, z, tri.simplices, intensity=z, colorscale=‘balance’, plot_edges=True)
File “”, line 50
data1 = plotly_triangular_mesh(x, y, z, tri.simplices, intensity=z, colorscale=‘balance’, plot_edges=True)
SyntaxError: invalid syntax

Am I doing something wrong? Is it just the current version of plotly that enters into conflict with that statement ?

@ifffam I’ve just downloaded both notebooks and ran each one with the last plotly version. No error is thrown.