Two colormaps in the same plot

Hi,
I am superimposing two images, here, I would like to use different colormap for first and second images. For e.g. jet for image 1 and hot for image 2..
I have tried this, however, at the end, both the images got the colromap defined for image 2. If somebody has gone through this kind of situtaion, please help me with this..

 Accepted Answer

Steve Eddins
Steve Eddins on 10 Nov 2020
The ColorBar object has an undocumented Colormap property. You can set this property directly to control the colors displayed by the ColorBar. You can use the documented properties such as Limits so that the display corresponds to your original data values.
Just be aware that the ColorBar's behavior with the respect to the Colormap property might change in a future release.
And don't tell anyone that I told you about this. ;-)

8 Comments

Haha, That's great trick... Thanks, steve for adressing this issue with patience..
Finally, how to display two colorbars while running below code, at present though I define two colorbars. In the final image I just getting one, Why it so ??
I=readimx('B00003.im7');
h = (I.Frames{1}.Components{1}.Planes {1,1})';
h1 = fix(h/10);
h1(h1==0) = 1 ;
rgb1 = ind2rgb(h1,jet(256));
ax1=gca;
G = imagesc(flipud(rgb1));
c = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
G.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal
hold on;
I1=readimx('B00045.im7');
h2 = (I1.Frames{2,1}.Components{1,1}.Planes {1,1})';
h3 =fix(h2/10);
h3(h3==0) = 1 ;
rgb2 = ind2rgb(h3,hot(256));
ax1=gca;
GH = imagesc(flipud(rgb2));
c1 = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
GH.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal
Probably the 2nd colorbar is on top of the first one. Use the ColorBar's Position property to control where they go. See ColorBar Properties in the doc.
Okay, Thanks.. I will look into this..
Hi Steve,
I played with colorbar position, Indeed, I am able to get the two colorbars (i.e. left , right side to the axis). However, the colormap pattern switches to same, though I defined 'jet' and 'hot' for image 1, 2 respectively.
I=readimx('B00003.im7');
% x=(I.x);
% y=(I.y);
h = (I.Frames{1}.Components{1}.Planes {1,1})';
h1 =fix(h/10);
h1(h1==0) = 1 ;
rgb1 = ind2rgb(h1,jet(256));
G = imagesc(flipud(rgb1));
ax1=gca;
colormap(ax1,'jet');
%caxis ([0 3000]);
%c = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
c = colorbar(ax1,'Location', 'east','FontSize',20,'TickLabelInterpreter', 'latex');
% c.Title.String = '$ OH(A.U)$';
c.Title.Interpreter = 'latex';
c.Title.FontSize = 16;
G.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal
hold on;
% linkaxes([ax1,ax2]);
I1=readimx('B00045.im7');
% x=(I1.x);
% y=(I1.y);
h2 = (I1.Frames{2,1}.Components{1,1}.Planes {1,1})';
h3 =fix(h2/10);
h3(h3==0) = 1 ;
rgb2 = ind2rgb(h3,hot(256));
GH = imagesc(flipud(rgb2));
ax2=gca;
colormap(ax2,'hot')
% caxis ([0 3000]);
%c1 = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
c1 = colorbar(ax2,'Location', 'west','FontSize',20,'TickLabelInterpreter', 'latex');
% c1.Title.String = '$ OH(A.U)$';
% c1.Title.Interpreter = 'latex';
% c1.Title.FontSize = 16;
GH.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal
As I mentioned in the answer, use the Colormap property of each colorbar to control its colors independently.
Steeve,
I did defined individually the color pattern and position as shown below. I am able to get the two colorbars in the defined position i.e. east and west, but not the pattern...
%%% First image
colormap(ax1,'jet');
c = colorbar(ax1,'Position',[.88 .11 .0275 .815],'Location', 'east','FontSize',20,'TickLabelInterpreter', 'latex');
%%% Second image
colormap(ax2,'hot')
c1 = colorbar(ax2,'Position',[.88 .11 .0275 .815],'Location', 'west','FontSize',20,'TickLabelInterpreter', 'latex');
I see now that it looks like your code is trying to create two different axes objects (ax1 and ax2), but it really only makes 1. The function gca returns the current axes, or it makes one if one does not already exist. So, this line:
ax2=gca;
does not make a second axes. It just returns the first. Because of that, you are not setting the colormaps of two different axes objects; rather, you are just overwriting the colormap of one.
I don't think you'll be able to get this transparent overlay idea to work in two different axes. My best suggestion is still to work in one axes and set the Colormap property of each colorbar directly, as in:
c1.Colormap = jet(256);
c2.Colormap = hot(256);
Great, Thanls a lot steve..
Now, I am getting two color bars

Sign in to comment.

More Answers (1)

You can't display images with two different colormaps within the same axes. As a workaround, you could convert both images from indexed to rgb, like this:
rgb1 = ind2rgb(X1,jet(256));
rgb2 = ind2rgb(X2,hot(256));
These two images can then be displayed or superimposed together. RGB image display is independent from the colormap.

11 Comments

Hi Steve,
Thanks for the quick response.
Here, in my case X1 and X2 are double type which got the inetnsity values of the image. As I tried to execute the convesrion of indexed to rgb, I got the below error. I have attached X1 and X2 for your refrence.
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in ind2rgb (line 26)
r = zeros(size(a)); r(:) = cm(a,1);
Scale and then round your X1 and X2 matrices so that they contain integers in the range 1 to 256.
Actually, I need to retain the true values. Is there any alternate way to do this ??
Can you clarify? Do you need the image CData values to be the same as the original data values? If so, for what purpose?
Yes, Correct, I need to retain Cdata value as same as original.
Actually, I have to write a comparitive decription based on the real intensity values. That mandates me to retain the values..
You can use a separate variable to store the scaled values and use that separate variable to display the image. The original values are retained in the original variable.
Yes, I tried that. However, I have to dispaly the images with real values, that's the constraint..
Why? Why is that a constraint for you?
Because I have to intrept something based on the image displayed with real intensity values...
Can you expand a little bit more in your replies? I don't think I'm getting enough information to be able suggest a workaround. Please say more about exactly what you are doing or displaying with the images so that it makes a difference what the CData values are.
I have attached the superimposed images plotted from real values (Capture2) and RGB values (Capture1). As you see from the images, the colormap of capture 2 clealry depicts the reals values (0 to 4000), however, the colormap of RGB shows the intensity values from 0 to 1.
Anyways, RGB image can be dipslayed independent of colormap, hence, I can display the colormap with real values, so this problem is solved.
Since, I have displayed the images using two color patterns i.e. jet, hot (see Capture 1), here, could you please tell me how to include the two color bars ???
%%%%%%%%%%% Image display with RGB values %%%%%%%%%%%% (Capture1)
I=readimx('B00003.im7');
h = (I.Frames{1}.Components{1}.Planes {1,1})';
h1 = fix(h/10);
h1(h1==0) = 1 ;
rgb1 = ind2rgb(h1,jet(256));
ax1=gca;
G = imagesc(flipud(rgb1));
c = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
G.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal
hold on;
I1=readimx('B00045.im7');
h2 = (I1.Frames{2,1}.Components{1,1}.Planes {1,1})';
h3 =fix(h2/10);
h3(h3==0) = 1 ;
rgb2 = ind2rgb(h3,hot(256));
ax1=gca;
GH = imagesc(flipud(rgb2));
c1 = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
GH.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal
%%%%%%%%%%% Image display with real values %%%%%%%%%%%% (Capture2)
I=readimx('B00003.im7');
h = (I.Frames{1}.Components{1}.Planes {1,1})';
ax1=gca;
colormap(hot);
G = imagesc(flipud(h));
c = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
set(gca, 'YDir','normal')
axis equal
hold on;
I1=readimx('B00045.im7');
h2 = (I1.Frames{2,1}.Components{1,1}.Planes {1,1})';
ax1=gca;
colormap(jet);
GH = imagesc(flipud(h2));
c1 = colorbar('FontSize',20,'TickLabelInterpreter', 'latex');
GH.AlphaData = 0.7;
set(gca, 'YDir','normal')
axis equal

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!