How do I plot colors for different mesh elements using trimesh?

조회 수: 16 (최근 30일)
Garrett West
Garrett West 2023년 11월 4일
이동: Dyuman Joshi 2023년 11월 4일
I have a 2D mesh that I want to plot, and it seems like trimesh() may be the best function for doing so. However, I'm not clear on the capabilities of this function with regard to colors. Is there a way to fill each mesh element with a different color according to some pre-defined array of element colors? Is there any way to make this work with a colorbar?
Thanks.
Edit: To be more specific, my mesh is already stored in the data structure expected by the trimesh() function: A triangle connectivity matrix, and a set of x and y points for the nodes. I have a value stored in a separate vector for each element in the connectivity matrix that I want to be able to plot as a color for that element.
  댓글 수: 3
Garrett West
Garrett West 2023년 11월 4일
이동: Dyuman Joshi 2023년 11월 4일
Thanks for your answer. I edited my question above to be more specific. The "Z" value in my case is not a function of the X and Y points, but rather a precomputed value for each triangular element.
Dyuman Joshi
Dyuman Joshi 2023년 11월 4일
이동: Dyuman Joshi 2023년 11월 4일
In that case -
%Example
%Sample data
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
%Triangulation object
T = delaunay(X,Y);
%trisurf plot
h = trisurf(T, X, Y, Z);
%Values for Color
C = rand([size(Z) 3]);
%Update the color data of the trisurf plot
h.CData = C;

댓글을 달려면 로그인하십시오.

채택된 답변

Garrett West
Garrett West 2023년 11월 4일
I figured it out. I scaled the element values to match up to a chosen colormap (jet) and did a trimesh() one element at a time, specifying the face color of each element. I had to set the Z value to zero to allow me to add a face color and edge color. Then I added a colorbar and scaled it appropriately. Thanks for the suggestions!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by