How do I put different color(maps) on a same plot ?

85 views (last 30 days)
I was wondering if it were possible to have different surface colors on the same plot. For instance, if I want to highlight a ribbon around a sphere with a specific colormap, and have the sphere be unicolor, having it as a reference. I'll put a bit of code to illustrate what I'm trying to do :
% Create the sphere
[X,Y,Z] = sphere;
% Scale it
X = Param.radius * X;
Y = Param.radius * Y;
Z = Param.radius * Z;
% Plot it
sph = surf(SphereAxis, X, Y, Z);
% Esthetics
colormap(cmap);
shading interp;
set(sph, 'FaceColor', [1, 0.85, 0.85]);
lighting gouraud;
daspect([1 1 1]); % change here
alpha 0.2;
light;
view([45 20]);
% colormap(cmap);
hold on;
% Add another plot to it afterwards :
% For instance
% su = surf(SphereAxis, squeeze(U(1, :, :)), squeeze(U(2, :, :)), squeeze(U(3, :, :)));
% Plot.plotreferences.D3.p1 = su;
% shading interp;
% colormap(su, cmap);
On each try of the colormap function, the sphere wouldn't take the desired color and have the colormap color instead. The last one (last line) didn't work because I can't refer a plot handle to a colormap. Is there another function that does what I want to do ? Ideally, I would like to color the plot that I add onto the sphere with respect to a certain function (e.g an energy density function) is there a neat way to achieve something like that ?
EDIT :
Runnable code :
figure('units', 'normalized', 'Outerposition', [0.2 0.2 0.6 0.6]);
cmap = winter;
%% First subplot : sphere alone
subplot(1, 2, 1)
ax1 = gca;
% Create the sphere
[X,Y,Z] = sphere;
sph = surf(ax1, X, Y, Z);
shading interp;
set(sph, 'FaceColor', [1, 0.85, 0.85]);
lighting gouraud;
daspect([1 1 1]); % change here
alpha 0.2;
light;
view([45 20]);
%% Define the ribbon :
N = 100;
el = linspace(-0.5, 0.5, N);
az = linspace(-pi, pi, N);
[A, E] = meshgrid(az, el);
R = ones(size(A));
[Xrib, Yrib, Zrib] = sph2cart(A, E, R);
%% second plot : ribbon and sphere
subplot(1, 2, 2)
ax2 = gca;
% Create the sphere
[X,Y,Z] = sphere;
sph = surf(ax2, X, Y, Z);
shading interp;
set(sph, 'FaceColor', [1, 0.85, 0.85]);
lighting gouraud;
daspect([1 1 1]); % change here
alpha 0.2;
light;
view([45 20]);
hold on;
colormap(cmap);
rib = surf(ax2, Xrib, Yrib, Zrib);
shading interp;
I think I identified the problem : when removing the last line it seems to do what I would want. Unfortunately, I would still like to have the shading feature in my plot. Is there a way to go around it and keep it nevertheless ?
Thank you for your time !
  6 Comments
Walter Roberson
Walter Roberson on 29 May 2021
caxis() controls the range of values that is mapped into the color map.
J. Alex Lee
J. Alex Lee on 29 May 2021
the color matrix would be literally RGB values...i dont know how much more control over color you could need.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 29 May 2021
How do I put different color(maps) on a same plot ?
You cannot do that, not in the form stated.
You can only use one colormap per axes.
You can create multiple axes, setting the 'color' of the overlaying axes to be 'none' so that the layers underneath show through.
If you use yaxis left and yaxis right then there is nothing stopping you from using the same range of values on the two axes, and so being able to use different colormaps for the objects.
But much of the time what you should do instead is do the mapping of values to color yourself, or by using the File Exchange contribution freezeColors https://www.mathworks.com/matlabcentral/fileexchange/7943-freezecolors-unfreezecolors . Use rescale() or mat2gray() on the data to get a range 0 to 1, then use im2uint8, then use ind2rgb() getting out an RGB version of the object.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!