timmr002
timmr002

Reputation: 57

Change Colorbar Scheme

I read in an image in MATLAB and display it using imagesc. I then set the colormap to grey.

On top of this image, I plot points with the jet colour scheme. How do I then display the jet colorbar, to correspond to the colours of the points plotted on top of the original image? I tried redefining the colorbar after all of the plots, but this changes the original grey scale image back to colours, which isn't desired.

Code:

%Create Figure with handle.
h5=figure('units','normalized','outerposition',[0 0 1 1]);
whitebg(h5,[0 0 0]);
subplot(2,5,1);
k=1;
for i=16:25
    subplot(2,5,k);
    imagesc(squeeze(ana(:,:,i)));
    title(['Z=',num2str(i)]);
    colormap gray
    axis equal
    k=k+1;
    colorbar
end

%Adapt colour values so that they are between 0 and 1. We want to scale
%both data sets equally, so we find the smallest value across Ix and Iy. We
%also find what will be the new largest value across Ix and Iy, after we
%add the magnitude of the smallest value to make all numbers greater than
%or equal to 0.
absolutemin=min(min(Ix(:,1)),min(Iy(:,1)));
absolutemax=max(abs(absolutemin)+(max(Ix(:,1))),abs(absolutemin)+max(Iy(:,1)));

%Add the smallest value, and divide by the largest maximum value for both Ix
%and Iy.
ixcolours=uint8(((Ix(:,1)+abs(absolutemin))/absolutemax).*255)+1;
iycolours=uint8(((Iy(:,1)+abs(absolutemin))/absolutemax).*255)+1;

mycolours=jet(256);
o=1;
for k=16:25; %For all 3D slices

    for i=1:471; %and for all x and y seed slices
        if k==seed_locs(i,3);
            subplot(2,5,o);
            hold all%go to the corresponding z subplot
            plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',mycolours(ixcolours(i),:),'MarkerEdgeColor',mycolours(ixcolours(i),:),'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
            %hold on
        end
    end

    for i=1:486;
        if k==test_locs(i,3);
            subplot(2,5,o);
            hold all
            plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',mycolours(iycolours(i),:),'MarkerEdgeColor',mycolours(iycolours(i),:),'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
%             hold on
        end

    end
    o=o+1; %go to the next z subplot
end
colormap jet
colorbar

Upvotes: 0

Views: 250

Answers (1)

NKN
NKN

Reputation: 6434

I think the following example can help you to improve your code. You firstly need to define two colormaps:

colormap([cool(64);gray(64)]);

Then let's say we have two different sets of datapoints to plot:

[X,Y,Z] = peaks(25);
h(1) = surf(X,Y,Z);hold on
h(2) = pcolor(X,Y,Z);

So the data is defined with two different handles. Now we need to make the CData using the minimum and maximum values.

cmin = min(Z(:));
cmax = max(Z(:));

C1 = min(64,round((64-1)*(Z-cmin)/(cmax-cmin))+1);   % CData for the first datapoints

C2 = 64+C1;    % CData for the second datapoints

Now we update the CDatas for each object.

set(h(1),'CData',C1);
set(h(2),'CData',C2);

Now you can set the CLim property of axes:

caxis([min(C1(:)) max(C2(:))])
colorbar;

enter image description here

Upvotes: 3

Related Questions