Smoothing 3D surface plot and its color gradient

조회 수: 20 (최근 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 CenterFile Exchange에서 Surface 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