필터 지우기
필터 지우기

selective legend

조회 수: 4 (최근 30일)
fabio freschi
fabio freschi 2012년 3월 26일
Hi everybody, I wrote a function to plot tetrahedral meshes of a finite element program. I was not satisfied by the built-in TETRAMESH because it seems that every faces (even internal ones) are plotted, while only surface faces are required to display the structure under study. With large meshes (millions of elements) there is a great improvement of performances by plotting only surface elements.
After some preliminary processing of the mesh data, the command to plot my data is PATCH.
Every triangular element has its own color according to the different material. Colors are passed to PATCH by the passing a color matrix to 'FaceVertexCData' propery. The color matrix contains the RGB code for each element. I would like to produce a legend, where the mesh color are reported, e.g. one colored box on the left of the material name. Of course I need only one row for each different color/material without repetitions. The following code should reproduce the problem
clear all, close all
% color list
collist = [
1 0 0
0 1 0
0 0 1
1 1 0
1 0 1
0 1 1];
% fake mesh data: object 1
d = [1 2];
[x,y,z] = meshgrid(d,d,d); % A cube
x = [x(:);1.5];
y = [y(:);1.5];
z = [z(:);1.5];
dt1 = DelaunayTri(x,y,z);
% fake mesh data: object 1
[x,y,z] = meshgrid(2*d,2*d,2*d); % A cube
x = [x(:);3.5];
y = [y(:);3.5];
z = [z(:);3.5];
dt2 = DelaunayTri(x,y,z);
% data structure (this part is not as smart as it could be, but it is only an example!)
P = [dt1.X; dt2.X]; % node coordiantes
sT = [freeBoundary(dt1); freeBoundary(dt2)+size(dt1.X,1)]; % surface triangles
M = [ones(size(freeBoundary(dt1),1),1); 2*ones(size(freeBoundary(dt1),1),1)]; % object codes
% get colors
icol = collist(M,:);
patch('Faces',sT,'Vertices',P,'FaceVertexCData',icol,'FaceColor','flat','EdgeColor','k');
axis equal, grid on
view([1 -1 1])
Here I would like a legend with two rows, the first with a red square and 'material 1' as text, the second with a green box and 'material 2' as text.
Does anyone have any suggestion?
Thanks in advance Fabio

채택된 답변

Oleg Komarov
Oleg Komarov 2012년 3월 26일
One object per legend entry:
% Colors
M1 = ones(size(freeBoundary(dt1),1),1);
M2 = 2*ones(size(freeBoundary(dt1),1),1); % object codes
icol1 = collist(M1,:);
icol2 = collist(M2,:);
% Patces
patch('Faces' ,freeBoundary(dt1),...
'Vertices' ,dt1.X,...
'FaceVertexCData',icol1,'FaceColor','flat','EdgeColor','k');
patch('Faces' ,freeBoundary(dt2),...
'Vertices' ,dt2.X,...
'FaceVertexCData',icol2,'FaceColor','flat','EdgeColor','k');
legend('Red','Green')
axis equal, grid on
view([1 -1 1])
  댓글 수: 1
fabio freschi
fabio freschi 2012년 3월 26일
Dear Oleg,
thanks for your answer. I adapted your solution to my code and it works perfectly. Given that the number of objects is not so large, the graphics run smootly.
Fabio

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by