How can I apply different colors to specific areas of a surface plot?

50 次查看(过去 30 天)
Hi all,
I have a surface that is plotted based on the below code:
gamma=5.5;
T=1/(gamma*40);
kh=0.1;
p=0.09;
delta=0.1;
ktau=0.04;
Kc=0.2;
Khat=0.000015;
Kp=0.3;
kb=0.4;
Vs=T*0.9;
v_pmm=T*0.07;
alph0=T*0.003;
alph1=T*0.01;
Ke=14;
ks=0.2;
Kf=T*40;
kplc=0.11;
ki=2;
tmax=200/T;
e=0.0016;
vss=Vs/e;
K=(Khat)/ktau^4;
alpha0=delta.*(alph0)/ktau^4;
alpha1=delta.*(alph1)/ktau^4;
v_pm=delta.*(v_pmm)/ktau^4;
tmaxhat=tmax*ktau^4;
[c,ct]=meshgrid(0:0.01:10);
A=(-(vss.*c.^2)./(ks.^2))+((Vs.*K.*gamma.^2.*ct.^2)./(ks.^2))+alpha0+alpha1.*((Ke.^4)./(Ke.^4+(gamma.*ct).^4));
h=-0.4.*A.*((Kc.^4).*(Kp.^2))./((p.^2.*c.^4.*gamma.*ct.*Kf));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[m,n] = size(c);
[in,im] = meshgrid(1:n-1,1:m-1);
ind = sub2ind([m,n],im(:),in(:));
tri = [[ind,ind+1,ind+m];[ind+1,ind+m+1,ind+m]];
xyz=[ct(:),h(:),c(:).*0.04];
normals = cross(xyz(tri(:,2),:) - xyz(tri(:,1),:),xyz(tri(:,3),:) - xyz(tri(:,1),:));
direction = normals(:,3) >= 0;
H1 = trimesh(tri(direction,:),xyz(:,1),xyz(:,2),xyz(:,3));
H1.FaceColor = 'r';
H1.EdgeColor = 'none';
H1.FaceAlpha=0.3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xlabel('ct')
ylabel('h')
zlabel('c')
xlim([0 1.5])
ylim([0 1.5])
zlim([0 0.05)
I want to use different colors to highlight a specific area on the surface, as shown with the hatched part in the attached figure. The four boundary values that define this area are listed below and labeled in the figure:
1-(ct,h,c)=(0.359,0.1573,0.010831)
2-(ct,h,c)=(1.364,0.147,0.0407)
3-(ct,h,c)=(0.361,0.938,0.011138)
4-(ct,h,c)=(0.7,0.915,0.022367)
I tried to do it with fill3 but it just plot a line not a curve that exactly be on the surface. The two parallel curve in the attached figure are plotted by importing data.
I would be grateful for any help.

回答(2 个)

Aman Banthia
Aman Banthia 2023-10-7
Hi M,
I understand that wanted to make the specific area stand out with a different color, similar to a hatched part in an attached figure. The goal is to adjust the MATLAB code to color this specific area on the 3D surface plot.
Here are a few steps to achieve what you are trying to do:
  1. Setting up the parameters: The code starts by defining a series of parameters that are used in the calculations. These include various constants and coefficients.
  2. Creating the meshgrid: The 'meshgrid' function is used to create a grid of "ct" and "c" values, which are used as the x and y coordinates for the surface plot.
  3. Calculating "h" values: The "h" values, which are used as the z coordinates for the surface plot, are calculated based on the given equations and parameters.
  4. Creating the triangular mesh: The code then creates a 3D triangular mesh from the ct, h, and c data. This is done using the 'meshgrid', 'sub2ind', and 'trimesh' functions. The 'cross' function is used to calculate the normals of the triangles, which are used to determine the direction of the triangles.
  5. Creating the mask: A logical mask is created for the specific area based on the given boundary values. This is done using logical indexing and the & operator.
  6. Setting the colors: The colors of the vertices are set based on the mask. This is done by creating a colormap and using the mask to index into this colormap.
  7. Creating the 3D triangular mesh with colors: The trimesh function is used again to create the 3D triangular mesh, this time with the colors. The 'FaceVertexCData' property is used to specify the colors of the vertices, and the 'FaceColor' property is set to 'flat' to color each face with a single color.
  8. Setting the labels and limits: Finally, the 'xlabel', 'ylabel', 'zlabel',' xlim', 'ylim', and 'zlim' functions are used to set the labels and limits of the axes.
Refer to the below code which contains the above mentioned changes:
% Specify the vertices of the polygon
xv = [0.359, 1.364, 0.361, 0.7];
yv = [0.1573, 0.147, 0.938, 0.915];
zv = [0.010831, 0.0407, 0.011138, 0.022367];
% Create the mask
mask = inpolygon(xyz(:,1), xyz(:,2), xv, yv) & (xyz(:,3) >= min(zv) & xyz(:,3) <= max(zv));
% Set the color of the vertices based on the mask
colormap = [1 0 0; 0 1 0]; % Red for the specific area, green for the other area
colors = colormap(mask+1, :); % Convert the logical mask to indices
% Create the 3D triangular mesh with the colors
H1 = trimesh(tri(direction,:),xyz(:,1),xyz(:,2),xyz(:,3), 'FaceVertexCData', colors, 'FaceColor', 'flat', 'EdgeColor', 'none');
H1.FaceAlpha=0.3;
You can make changes to the mask properties to gain a more accurate version of the plot you are trying to achieve.
Refer to the following links to know more about the function I have used in the code:
Hope the above solution helps.
Best Regards,
Aman Banthia

Image Analyst
Image Analyst 2023-10-7
You can make an image of the colors that you want to have at each location. Then see my attached demo.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by