Smoothing 3D surface plot and its color gradient

30 views (last 30 days)
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]);

Accepted Answer

Star Strider
Star Strider on 6 Sep 2023
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 Comments
Hariharan Siva
Hariharan Siva on 6 Sep 2023
Perfect! This is exactly what I was looking for. 😍 Thank you once again!

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!