Make a surface plot
3 views (last 30 days)
Show older comments
Edin Michael
on 7 Apr 2024
Commented: Star Strider
on 17 Apr 2024
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.
0 Comments
Accepted Answer
Star Strider
on 7 Apr 2024
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')
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
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)
[Cm1,CM2] = bounds(Cm(:))
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
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!