Make a surface plot
28 ビュー (過去 30 日間)
古いコメントを表示
Edin Michael
2024 年 4 月 7 日 8:59
コメント済み: Star Strider
2024 年 4 月 17 日 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.
0 件のコメント
採用された回答
Star Strider
2024 年 4 月 7 日 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')
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!