Smoothing 3D surface plot and its color gradient

20 次查看(过去 30 天)
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!

请先登录,再进行评论。

更多回答(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