Clear Filters
Clear Filters

Make a surface plot

28 views (last 30 days)
Edin Michael
Edin Michael on 7 Apr 2024 at 8:59
Commented: Star Strider on 17 Apr 2024 at 2:59
I need to make a surface plot where I also need to colour the surface formed from the x,y and z coordinates based on a fourth variable, c.
Is there any way to do this.
I am attaching the excel file with the variables in them.

Accepted Answer

Star Strider
Star Strider on 7 Apr 2024 at 12:53
Creating surface plots from your data is straightforward using the scatteredInterpolant function. Once the ‘Cm’ matrix is created (and it has to be created using the same interpolation method that created ‘Zm’), the colour mapping to the surface works, and matches reasonably well with the initial scatter3+stem3 plot that I created to see how the final result should look (and to see if there was any pattern to the data that does not appear to have any patterns). The key to getting the colours to work was to use the interpolated colour matrix ‘Cm’ and clim.
Try this —
T1 = readtable('Surface plot data.xlsx')
T1 = 128x4 table
x y z c ____ ____ ______ ______ 3500 1300 0.3 370.66 6000 2200 0.45 316.32 7250 1750 0.375 327.03 4750 2650 0.525 305.79 5375 1975 0.4875 323.89 7875 2875 0.3375 295.35 6625 1525 0.5625 329.26 4125 2425 0.4125 321.77 4437 1862 0.5812 324.85 6937 2762 0.4312 299.4 8187 1412 0.5062 330.26 5687 2312 0.3562 318.33 5062 1637 0.3937 342.51 7562 2537 0.5437 296.44 6312 2087 0.3187 321.77 3812 2987 0.4687 306.8
figure
stem3(T1.x, T1.y, T1.z)
hold on
scatter3(T1.x, T1.y, T1.z, 75, T1.c, 'filled')
hold off
cm = colormap(turbo(height(T1)));
CL1 = clim
CL1 = 1x2
285.5780 370.6595
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Scatter + Stem Plot')
xv = linspace(min(T1.x), max(T1.x), fix(size(T1,1)));
yv = linspace(min(T1.y), max(T1.y), fix(size(T1,1)));
[Xm,Ym] = ndgrid(xv, yv); % Create Interpolation Matrices
Ci = scatteredInterpolant(T1.x, T1.y, T1.c, 'natural','none'); % Colour Matrix
F = scatteredInterpolant(T1.x, T1.y, T1.z, 'natural','none'); % 'Z' Matrix
Cm = Ci(Xm,Ym);
Zm = F(Xm,Ym);
[c1,c2] = bounds(T1.c)
c1 = 285.5780
c2 = 370.6595
[Cm1,CM2] = bounds(Cm(:))
Cm1 = 285.5890
CM2 = 370.6595
figure
surfc(Xm, Ym, Zm, Cm)
clim(CL1)
colormap(cm)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Surface — ‘natural’ Method, No Extrapolation')
Ci = scatteredInterpolant(T1.x, T1.y, T1.c, 'linear','none');
F = scatteredInterpolant(T1.x, T1.y, T1.z, 'linear','none');
Cm = Ci(Xm,Ym);
Zm = F(Xm,Ym);
figure
surfc(Xm, Ym, Zm, Cm)
clim(CL1)
colormap(cm)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Surface — ‘linear’ Method, No Extrapolation')
Ci = scatteredInterpolant(T1.x, T1.y, T1.c, 'nearest','none');
F = scatteredInterpolant(T1.x, T1.y, T1.z, 'nearest','none');
Cm = Ci(Xm,Ym);
Zm = F(Xm,Ym);
figure
surfc(Xm, Ym, Zm, Cm)
clim(CL1)
colormap(cm)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Surface — ‘nearest’ Method, No Extrapolation')
The same general approach is repeated with each interpolation method.
My code does not extrapolate. There is no harm in setting an extrapolation method — I am simply averse to extrapolating.
.
  2 Comments
Edin Michael
Edin Michael on 17 Apr 2024 at 1:29
Thanks a lot for the help.
Star Strider
Star Strider on 17 Apr 2024 at 2:59
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!