Ander Biguri
Ander Biguri

Reputation: 35525

How to set colorbar labels

I have some points in a 'jet' colormap. The points have a coefficient that can go from 0 to 1, but usually they dont cover all the range, e.g 0.75-0.9.

When I plot those points I colour them so 0.75 is the lesser colour in the colormap and 0.9 is the maximum color in the colormap, so all the colormap is shown. What I want to do is show that in the colorbar also. When I plot the colorbar the labels on it go to 64, but I want them from 0.75 to 0.9. How can I do that?

EDIT I don't think the code itself helps a lot but here it goes, just in case. In the colors variable I convert the ZNCC to the range of the colormap.

EDIT2

I found the reason why caxis is not working for me. Here is the code:

%this is why it doesnt work
im=imread('someimageyouwanttotest_inRGB.png')
imshow(im)


points=[1, 2;1 , 2 ;0.3,0.7]
ZNCC=points(3,:)
cmap=colormap('jet');
colors=cmap(round(  ((1-min(ZNCC))+ZNCC-1).*(size(cmap,1)-1)/max((1-min(ZNCC))+ZNCC-1))+1,:  );
hold on
for i=1:length(ZNCC)

    plot(points(1,i),points(2,i),'.','Color',colors(i,:));  

end
colorbar()
hold off

Upvotes: 1

Views: 8173

Answers (2)

Dan
Dan

Reputation: 45741

I think that is your code displays all your colours correctly then rather just set up the colour bar first on no image:

points=[1, 2;1 , 2 ;0.3,0.7]
ZNCC=points(3,:)

cmap=colormap('jet');
caxis([min(ZNCC) max(ZNCC)]); 
colorbar();

hold on

%this is why it doesnt work
im=imread('someimageyouwanttotest_inRGB.png')
imshow(im)

colors=cmap(round(  ((1-min(ZNCC))+ZNCC-1).*(size(cmap,1)-1)/max((1-min(ZNCC))+ZNCC-1))+1,:  );

for i=1:length(ZNCC)

    plot(points(1,i),points(2,i),'.','Color',colors(i,:));  

end

hold off

I can't test it as I don't have imshow :/

Upvotes: 1

wakjah
wakjah

Reputation: 4551

If caxis is not working for you, you could store the return from colorbar - it is a handle to the colorbar object. Then you can set its properties, like 'YTick' and 'YLim'. The full list of properties you can set is the same as the Axes Properties (because the colorbar is just an axes object, after all).

Here is an example:

% Generate some random data
z = rand(10);
[x, y] = meshgrid(1:size(z, 1));

% Plot colour map
pcolor(x, y, z);
shading interp; % Comment out to disable colour interpolation
colormap jet;

% Setup colorbar
c = colorbar();
set(c, 'YTick', [0.75 0.875 1]); % In this example, just use three ticks for illustation
ylim(c, [0.75 1]);

It is only necessary to do this once, after you've finished plotting.

Edit: If you need the limits and ticks automatically from the data, then you can do something like

% Find the limits
lims = [min(z(:)) max(z(:))]; 

% Function for rounding to specified decimal places
dprnd = @(x, dps)round(x*(10.^dps))./(10.^dps);

% Generate ticks
nTicks = 5; 
nDps = 2;
ticks = dprnd(linspace(lims(1), lims(2), nTicks), nDps);

set(c, 'YTick', ticks);

Upvotes: 1

Related Questions