-fmesh- face and edge color different for neg, 0, pos Z values

1 view (last 30 days)
Hi folks!
I am making a 3-D plot using -fmesh-.
I would like to make the face and edge colors different for negative, zero, and positive Z values.
I have tried the following so far:
s=fmesh(@(X,Y) X./(Y.*(1-X)-1));
s.FaceColor = 'interp';
s.EdgeColor = 'white';
EC = s.EdgeColor;
FC = s.FaceColor;
But, I am not sure how to creat the conditional statement to access 1x1 FunctionSurface to make these changes.
I tried something like below, which did not work, but the idea was to possibly plot only positive ZData values.
s.ZData = s.ZData(s.ZData >= 0);
What I want to obtain are:
  1. Have three distinct -s.EdgeColor- and -s.FaceColor- for negative, zero, and positive Z values.
  2. Draw three separate -fmesh- plots only for negative, zero, and positive Z values all separately.
Thanks in advance!

Accepted Answer

Walter Roberson
Walter Roberson on 1 Sep 2022
You can color by z by creating a colormap that divides the range up into segments, and setting the first half to the color for negative, the last half to the color for positive, and the middle to the color for zero.
Note that this will end up plotting a range of values with the zero color, rather than the values that are exactly zero.
FunctionSurface objects do not have the ability to set the color for individual locations.
s=fmesh(@(X,Y) X./(Y.*(1-X)-1));
s.FaceColor = 'interp';
s.EdgeColor = [0 .3 0];
[minz, maxz] = bounds(s.ZData(:));
zrange = max(abs([minz, maxz]));
negcol = [0 0 0.7];
zcol = [.8 .8 .8];
poscol = [.7 0 0];
cmap = repelem( [negcol; zcol; poscol], [127 1 128], 1);
caxis([-zrange zrange]);
colormap(cmap)
  7 Comments
Walter Roberson
Walter Roberson on 2 Sep 2022
[C, h]=contourf(X,Y,Z);
You cannot do that directly. fmesh() gives you vectors of XData and YData and ZData, but for contourf() you need to have 2D arrays of data.
You could switch out of using fmesh() into creating your own grids and using contour3(), but if you wanted to proceed with fmesh() you would have to use scattered interpolant techniques to create the data to contour

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!