How to fill different faces with different colors

9 views (last 30 days)
clear;
[X Y]=meshgrid(10:10:1000);
ind1=find(X<=Y&0<Y&Y<=400&X>0);%索引行和列
ind2=find(X<=Y&Y>400&X>0);
ind3=find(X>Y+70&0<Y&Y<=400&X>0);
ind4=find(Y+70>=X&X>Y&0<Y&Y<=400&X>0);
ind5=find(X>Y+70&Y>400&X>0);
ind6=find(Y+70>=X&Y>400&X>Y&X>0);
P=zeros(100);
P(ind1)=2973.29*X(ind1)-97.44*Y(ind1);
P(ind2)=5773.29*X(ind2) - 3067.69*Y(ind2)+ 80077.78;
P(P==0)=NaN;
P(ind3)=-1895.48*X(ind3)+ 4783.77*Y(ind3)+ 303479.50;
P(ind4)=2726.84*X(ind4)+ 161.4 图 4*Y(ind4)- 20083.0;
P(ind5)=-1895.48*X(ind5)+ 4583.77*Y(ind5)+ 383479.50;
P(ind6)=2726.85*X(ind6)- 40.0*Y(ind6)+ 60494.78;
mesh(X,Y,P);
the code is as above
and how could i fill different faces with different colors?

Answers (1)

Pavan Sahith
Pavan Sahith on 12 Feb 2024
Hello,
I see that you're exploring how to assign different colors to various faces in a mesh plot.
A straightforward way to achieve this is by utilizing the 'Facecolor' property of the mesh object.
Below is a snippet of MATLAB code that you can integrate into your existing code to enhance your mesh plot with distinct face colors:
figure;
mesh(X, Y, P);
colormap jet;
% Set face colors
cdata = zeros(size(P));
cdata(ind1) = 1;
cdata(ind2) = 2;
cdata(ind3) = 3;
cdata(ind4) = 4;
cdata(ind5) = 5;
cdata(ind6) = 6;
set(mesh(X, Y, P), 'FaceColor', 'flat', 'CData', cdata);
colorbar;
xlabel('X');
ylabel('Y');
In this code,
  • The 'cdata' matrix is created to assign different colors to different faces of the mesh plot.
  • The 'cdata' matrix is set as the 'CData' property of the mesh object.
  • The 'FaceColor' property is set to 'flat' to use the assigned colors for each face.
To know more about the 'mesh' properties and 'FaceColor', you can refer to the following MathWorks documentation.
Hoping this will help.

Community Treasure Hunt

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

Start Hunting!