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

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 comentarios

Adam Danz
Adam Danz el 28 de Mayo de 2021
We can't run your code due to missing variable definitions.
Gronaz
Gronaz el 28 de Mayo de 2021
Okay, sorry about that, I added the code leaving it as a reference knowing that it was in the middle of a function and wouldn't run, I'll edit the question so that there's a runnable code part.
J. Alex Lee
J. Alex Lee el 29 de Mayo de 2021
surf() will accept a rgb color matrix as an argument after the coordinate positions, is that what you could be after?
Gronaz
Gronaz el 29 de Mayo de 2021
Thank you for your reply !
Is it possible to assign a colorbar to that color matrix ? And is there a way to map matrix values to a rgb color matrix ? Say, I computed the values of a density (for instance) on a grid, can I map them to colors that depend on a range from CLim(1) to CLim(2), which is wider than the max and min values of that density ?
caxis() controls the range of values that is mapped into the color map.
J. Alex Lee
J. Alex Lee el 29 de Mayo de 2021
the color matrix would be literally RGB values...i dont know how much more control over color you could need.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Mayo de 2021

0 votos

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.

Más respuestas (0)

Categorías

Productos

Versión

R2021a

Preguntada:

el 28 de Mayo de 2021

Comentada:

el 29 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by