Smoothing 3D surface plot and its color gradient

18 ビュー (過去 30 日間)
Hariharan Siva
Hariharan Siva 2023 年 9 月 6 日
コメント済み: Star Strider 2023 年 9 月 6 日
I am currently facing trouble replicating the attached figure.
In specific, I am facing difficulties in achieving a seamless 3D surface plot, as well as its transitioning from discrete to continuous color gradients. In my efforts to smooth the surface, I have tried with increasing the data point density and interpolations to enhance its smoothness. But it yielded a somewhat jagged effect. Same goes to the color scale.
Any assistance in resolving these issues would be highly appreciated.
filename = 'Data.csv';
data = readmatrix(filename);
x = data(:, 1);
y = data(:, 2);
z = data(:, 3);
% Create a mesh grid for X and Y
[X, Y] = meshgrid(x, y);
% Create a mesh grid for Z
Z = meshgrid(z);
% Add a vertical line at z = 0 by making a mesh grid
Z0 = zeros(size(X));
% 3D surface plot
figure;
set(gcf, 'Color', [1, 1, 1]);
surf(X, Y, Z, 'EdgeColor', 'none');
lineColors = [...
90, 0, 9
120, 25, 7
157, 56, 10
187, 97, 45
202, 129, 88
216, 163, 131
231, 198, 179
255, 255, 255
191, 215, 227
132, 180, 203
75, 143, 180
25, 107, 154
3, 75, 133
2, 47, 115
1, 18, 98
]./255;
vik = interp1(1:max(size(lineColors)),lineColors,1:(1/4):max(length(lineColors)));
colormap(flipud(vik));
% colorbar;
caxis([-0.6, 1]);
% X-axis limits to match data
xlim([min(x), max(x)]);
ylim([-2, 2]);
hold on;
% Add a grey section
surf(X, Y, Z0, 'EdgeColor', 'none', 'FaceColor', [0.5, 0.5, 0.5], 'FaceAlpha', 0.2);
hold off;
% Remove gridlines, axis labels, and tick marks for y and z axes
grid off;
set(gca, 'YTick', []);
set(gca, 'ZTick', []);
set(gca, 'YColor', 'none');
set(gca, 'ZColor', 'none');
grid on;
set(gca, 'GridColor', [0.5, 0.5, 0.5]);

採用された回答

Star Strider
Star Strider 2023 年 9 月 6 日
Experiment with the daspect function —
filename = 'Data.csv';
data = readmatrix(filename);
x = data(:, 1);
y = data(:, 2);
z = data(:, 3);
% Create a mesh grid for X and Y
[X, Y] = meshgrid(x, y);
% Create a mesh grid for Z
Z = meshgrid(z);
% Add a vertical line at z = 0 by making a mesh grid
Z0 = zeros(size(X));
% 3D surface plot
figure;
set(gcf, 'Color', [1, 1, 1]);
surf(X, Y, Z, 'EdgeColor', 'none');
lineColors = [...
90, 0, 9
120, 25, 7
157, 56, 10
187, 97, 45
202, 129, 88
216, 163, 131
231, 198, 179
255, 255, 255
191, 215, 227
132, 180, 203
75, 143, 180
25, 107, 154
3, 75, 133
2, 47, 115
1, 18, 98
]./255;
vik = interp1(1:max(size(lineColors)),lineColors,1:(1/4):max(length(lineColors)));
colormap(flipud(vik));
% colorbar;
caxis([-0.6, 1]);
% X-axis limits to match data
xlim([min(x), max(x)]);
ylim([-2, 2]);
hold on;
% Add a grey section
surf(X, Y, Z0, 'EdgeColor', 'none', 'FaceColor', [0.5, 0.5, 0.5], 'FaceAlpha', 0.2);
hold off;
% Remove gridlines, axis labels, and tick marks for y and z axes
grid off;
set(gca, 'YTick', []);
set(gca, 'ZTick', []);
set(gca, 'YColor', 'none');
set(gca, 'ZColor', 'none');
grid on;
set(gca, 'GridColor', [0.5, 0.5, 0.5]);
daspect([10 1 1]) % <— ADDED
It changes the aspect ratio of the plot axes.
.
  4 件のコメント
Hariharan Siva
Hariharan Siva 2023 年 9 月 6 日
Perfect! This is exactly what I was looking for. 😍 Thank you once again!
Star Strider
Star Strider 2023 年 9 月 6 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by