The first problem is quite simple. You just need to specify the limit of the color axis. If you set the same limits for all axes, then, value at the same level will have the same color. Try this code
[X,Y] = meshgrid(linspace(-1/2*pi,1/2*pi));
Z1 = cos(X).*cos(Y);
Z2 = 2*cos(X).*cos(Y);
figure;
subplot(1,2,1);
surface(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2])
subplot(1,2,2);
surface(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2])
figure;
subplot(1,2,1);
surf(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2])
subplot(1,2,2);
surf(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2])
The second problem is a bit complicated. You need to create your own colormap such that the color appears to be discrete. Try the following code. I divided the interval [0,2] into 5 portions. This code used random colors for each portion, you can define your own RGB colors equal to the number of portions.
[X,Y] = meshgrid(linspace(-1/2*pi,1/2*pi));
Z1 = cos(X).*cos(Y);
Z2 = 2*cos(X).*cos(Y);
color_edges = [0 0.4 0.8 1.2 1.6 2.0];
colors = rand(5,3);
resolution_colormap = 100;
t = linspace(min(color_edges), max(color_edges), resolution_colormap);
c_idx = discretize(t, color_edges);
c_map = colors(c_idx, :);
figure;
subplot(1,2,1);
surface(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
subplot(1,2,2);
surface(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
figure;
subplot(1,2,1);
surf(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
subplot(1,2,2);
surf(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
Best Answer