How to plot a 3D surface out of points, and set explicit colors for specific value ranges

I have matrices x, y, z, and s, which have dimensions 2500x1. I'm trying to make a surface plot out of x, y, z. I tried this:https://www.mathworks.com/matlabcentral/answers/384291-3d-surface-plot-out-of-points#answer_306637. But is it possible to set plot colors for specific value ranges of s (given in the table below)? Like by applying a colormap or something?
I have defined the colormap as follows:
map = [0 1 0;
0 1 1;
0 0 1;
0 0.5 1;
1 1 1;
1 1 0.5;
1 1 0;
1 0.5 0;
1 0 0];
colormap(map);
caxis([-1 1]);
As an example, I need the plot to look similar to this:

2 Comments

Your code looks correct. What is the problem?
I can't get the colors right. I was thinking that maybe someone could give a hint on how to correctly pass the colormap into the trisurf() function, or suggest a different method of plotting. This is my code:
dt = delaunayTriangulation(x,y);
tri = dt.ConnectivityList;
xi = dt.Points(:,1);
yi = dt.Points(:,2);
F = scatteredInterpolant(x,y,z);
zi = F(xi,yi);
map = [0 1 0;
0 1 1;
0 0 1;
0 0.5 1;
1 1 1;
1 1 0.5;
1 1 0;
1 0.5 0;
1 0 0];
figure
surface = trisurf(tri,xi,yi,zi);
shading interp
colormap(map);
caxis([-1 1]);
grid on
colorbar
hold on
This is what I'm getting, which is not correct:

Sign in to comment.

Answers (2)

It is obvious from your image that you are not setting the limits of caxis properly. Your code
caxis([-1 1]);
but your image show that value vary in range [-4000, 4000]
Try
caxis([-4000 4000]);

4 Comments

But s is supposed to be between [-1 1], not [-4000 4000]. The color needs to be tied to the values of s, not z.
Can you show your code? How is variable s being used?
Well, s isn't being used at the moment. That's what I can't figure out how to use with a 3D surface. I have a text file with the surface data, where each row has 4 values separated by commas (x, y, z, and s respectively). My code:
clear all;
path_to_txt = 'Surface_Data.txt';
surface_data = readtable(path_to_txt);
x = table2array(surface_data(:,1));
y = table2array(surface_data(:,2));
z = table2array(surface_data(:,3));
s = table2array(surface_data(:,4));
dt = delaunayTriangulation(x,y);
tri = dt.ConnectivityList;
xi = dt.Points(:,1);
yi = dt.Points(:,2);
F = scatteredInterpolant(x,y,z);
zi = F(xi,yi);
map = [ 0 1 0 ;
0 1 1 ;
0 0 1 ;
0 0.5 1 ;
1 1 1 ;
1 1 0.5;
1 1 0 ;
1 0.5 0 ;
1 0 0 ];
figure
surface = trisurf(tri,xi,yi,zi);
shading interp
colormap(map);
caxis([-1 1]);
grid on
colorbar
hold on
The color is taken from the colormap according to whatever variable is on the z-axis and the value of CLim for the color axis. If you want the colormap to be linked to s, then you need to use s in place of z in your code.
What do the four columns of your dataset represent? Maybe that can help in determining how the s value can be used.

Sign in to comment.

Your map is wrong. You did not take into account that the first and last slots are half the size of the other slots.
map = [ 0 1 0 ; %-1 to -7/8 %green
0 1 1 ; %-7/8 to -6/8 %cyan
0 1 1 ; %-6/8 to -5/8
0 0 1 ; %-5/8 to -4/8 %blue
0 0 1 ; %-4/8 to -3/8
0 0.5 1 ; %-3/8 to -2/8 %pale blue
0 0.5 1 ; %-2/8 to -1/8
1 1 1 ; %-1/8 to 0 %white
1 1 1 ; %0 to +1/8
1 1 0.5; %+1/8 to +2/8 %pale yellow
1 1 0.5; %+2/8 to +3/8
1 1 0 ; %+3/8 to +4/8 %yellow
1 1 0 ; %+4/8 to +5/8
1 0.5 0 ; %+5/8 to +6/8 %orange
1 0.5 0 ; %+6/8 to +7/8
1 0 0 ]; %+7/8 to +1 %red

Categories

Products

Release

R2019b

Asked:

on 14 Mar 2020

Edited:

on 15 Mar 2020

Community Treasure Hunt

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

Start Hunting!