Open Huite opened 2 years ago
Datashader already supports weights on the triangles as well as on the vertices. Here is an example showing both:
import datashader as ds
import pandas as pd
canvas = ds.Canvas(plot_width=200, plot_height=200)
# Vertex-based weights.
vertices = pd.DataFrame(
[[0, 0, 1.0], [1, 0, 2.5], [0, 1, 1.5], [-1, 0, 0.5], [0, -1, 0.75]],
columns=("x", "y", "vert_weight"))
triangles = pd.DataFrame(
[[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 1]],
columns=["v0", "v1", "v2"])
agg = canvas.trimesh(vertices, triangles, agg=ds.sum("vert_weight"))
im = ds.transfer_functions.shade(agg, how="linear")
ds.utils.export_image(im, "vert_weight")
# Triangle-based weights.
vertices = pd.DataFrame(
[[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]],
columns=("x", "y"))
triangles = pd.DataFrame(
[[0, 1, 2, 0.5], [0, 2, 3, 1.0], [0, 3, 4, 1.5], [0, 4, 1, 2.0]],
columns=["v0", "v1", "v2", "tri_weight"])
agg = canvas.trimesh(vertices, triangles, agg=ds.sum("tri_weight"))
im = ds.transfer_functions.shade(agg, how="linear")
ds.utils.export_image(im, "tri_weight")
and the images produced:
Weights are taken from the vertices DataFrame if there are more than 2 columns, otherwise they are taken from the triangles DataFrame.
Certainly the docs could be more explicit about this.
A PR adding the above example to the Datashader docs would be very welcome, @Huite !
Great! -- I'll put the PR on my (somewhat crowded) to do list!
Is your feature request related to a problem? Please describe.
(I think this question applies to the Holoviews/Geoviews trimesh as well.)
Having the option to efficiently visualize large irregular meshes via datashader is quite useful. However, unless I'm mistaken, the trimesh currently only supports data on the nodes. This does the trick for e.g. finite element computation, but not for finite volume, where the data is associated with the simplex/face/cell (more similar to raster data).
(If my understanding is incorrect, I'd be happy to hear how to do it -- I think neither the docs/examples nor the source code show it in that case!)
Describe the solution you'd like
Ideally, I can just give some data that matches the length of the simplices, and get a triangle-by-triangle fill. For datashader, I suppose you could stick it in an additional column of the simplices dataframe.
With matplotlib, I can plot both data associated with the nodes via
triplot
or data on the simplices withPolyCollection
(although the latter one seems a bit obscure).Describe alternatives you've considered
Any alternative seems rather impractical (e.g. I could create a voronoi tesselation, then triangulate it to get data associated with the cell centroids to get something on the nodes).
Add any other context or screenshots about the feature request here.
I guess this relates somewhat to https://github.com/holoviz/holoviews/issues/3812.
It's relatively straightforward to triangulate a mesh of (convex) cells in a vectorized manner, storing the indices to grab the values, e.g.:
This would allow plotting of any mesh (triangles, quads, or even voronoi cells).
It should be easier than the node based visualization: there's no need to interpolate values inside of the triangles, just sampling suffices.