Clear Filters
Clear Filters

color for each surfplot in a plot with multiple surf

5 views (last 30 days)
Hi,
I have multiple surf and scatter3 plots in the same figure in order to generate 3d-Pareto Front.
I would like to execute a colormap() for the plot in order to have different color for each surfaceplot.
What I Did, i put the color for each surface, manually.
How can I obtain automaic color foe each surface.
h20=surf(XX20,YY20,ZZ20,'LineStyle','none','FaceColor','k', 'FaceAlpha',0.5, 'EdgeColor','none')
hold on
scatter3(XF20(:,1),XF20(:,2),XF20(:,3),'k*');
hold on
h40=surf(XX40,YY40,ZZ40,'LineStyle','none','FaceColor','m', 'FaceAlpha',0.5, 'EdgeColor','none')
hold on
scatter3(XF40(:,1),XF40(:,2),XF40(:,3),'ko');
hold on
h60=surf(XX60,YY60,ZZ60,'LineStyle','none','FaceColor','y' ,'FaceAlpha',0.5, 'EdgeColor','none')
hold on
scatter3(XF60(:,1),XF60(:,2),XF60(:,3),'k+');
hold on
h80=surf(XX80,YY80,ZZ80,'LineStyle','none','FaceColor','c', 'FaceAlpha',0.5, 'EdgeColor','none')
hold on
scatter3(XF80(:,1),XF80(:,2),XF80(:,3),'kd');
hold on
h100=surf(XX100,YY100,ZZ100,'LineStyle','none','FaceColor','g', 'FaceAlpha',0.5, 'EdgeColor','none')
hold on
scatter3(XF100(:,1),XF100(:,2),XF100(:,3),'k<');
hold on
h120=surf(XX120,YY120,ZZ120,'LineStyle','none','FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','none')
hold on
scatter3(XF120(:,1),XF120(:,2),XF120(:,3),'kx');
hold on
h140=surf(XX140,YY140,ZZ140,'LineStyle','none','FaceColor','b', 'FaceAlpha',0.2, 'EdgeColor','none')
hold on
scatter3(XF140(:,1),XF140(:,2),XF140(:,3),'ks');

Answers (1)

Neelanshu
Neelanshu on 30 Jan 2024
Hi Regaieg,
I understand from your query that you are interested in using colormap to have different colored surface plots.
In MATLAB, when you create multiple surface (surf) plots and scatter3 plots in the same figure, each plot typically gets its own color based on the current colormap. However, if you want to ensure that each surface has a distinct color automatically without manually setting the "FaceColor", you can modify the "CData" property of each surface to be a matrix of the same value, which will cause the surface to appear as a uniform color as shown in the code snippet below:
% Create a figure
figure;
% Define your data for multiple surfaces (example data)
[X1, Y1] = meshgrid(1:10, 1:10);
Z1 = peaks(10);
[X2, Y2] = meshgrid(1:10, 1:10);
Z2 = Z1 + 5;
% Create the first surface plot
surf1 = surf(X1, Y1, Z1);
hold on; % Hold on to plot additional surfaces
% Create the second surface plot
surf2 = surf(X2, Y2, Z2);
% Set the CData property for each surface to a constant value
% The constant value should be within the range of the colormap
surf1.CData = ones(size(Z1)) * 1; % This will use the first color of the colormap
surf2.CData = ones(size(Z2)) * 2; % This will use the second color of the colormap
% Optionally, you can add scatter3 plots or more surfaces here
% Update the colormap to have as many colors as you have surfaces
colormap(parula); % 'parula' can be replaced with any other colormap
% Release the hold on the figure
hold off;
You have to ensure that the constant values you assign to "CData" should be within the range of the colormap you're using. If you have a lot of surfaces, you might need to use a colormap with a broad range of colors, or you can create a custom colormap that suits your needs.
Hope this helps.

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!