0
votes

I've developed a new R,G,B colormap of 15 entries based on my data. These entries varying in the range [0.0,1.0] in a monotonically growing sequence and represent the value of a parameter P to which 15 samples are associated. For instance, 3 pairs (P,sample number) would be (0.0,266); (0.5,262); (1.0,276).

Problem: I wish that P appear from red to blue in the colorbar, but that the 'ticks' mark the numbers for the 15 samples.

It's discussed in this post how to manipulate the colorbar's 'YTickLabel'.

In this second post from MathWorks , the solution to change the amount of ticks is readily achieved by calling the colorbar handle cbr and using set(cbr,'YTick',20:2:50) - as discussed therein. However, sounds to me that both ways are fine since the the data are monotonic and with small amount of ticks.

In my case, P grows monotonically, but the sample numbers associated to them do not. Also, there are 15 ticks.

Then, someone has a suggestion on how can I have these 15 tick marks correctly placed and working together with the location 'southoutside' to have the colorbar in the horizontal?

My attempts are below: For both cases, colorbar didn't print more than 8 ticks. I had no success with 'YTick' too, since the sample numbers are not monotonic.

  1. More "fancy" way:

    samplex = [ 266, 267, 265, ..., 262, 276 ]; % vector of 15 sample numbers 
    sx = num2str(samplex);
    sx = cellstr(sx);
    hx = colorbar;
    set(hx,'YTickLabel',sx)
    
  2. Add point by point

    hx = colorbar;
    set(hx,'YTickLabel',{'266','262',...,'276'})
    

Colormap matrix: this is my colormap matrix named CX in format short:

CX = 
    0.5397         0         0
    1.0000    0.4603         0
    1.0000    0.5714         0
    0.4603    1.0000    0.5397
    0.4286    1.0000    0.5714
         0    0.9206    1.0000
         0    0.2857    1.0000
         0    0.1905    1.0000
         0    0.1905    1.0000
         0    0.0476    1.0000
         0         0    0.9683
         0         0    0.9683
         0         0    0.9524
         0         0    0.7143
         0         0    0.5079

Then, I use

cx = colormap(CX); 
axis off; 
set(gcf,'color','None');

before the set commands quoted in my attempts.

Thank you for any help.

Remark: I just need to set up the colorbar and export it to a .pdf afterwards, with no background color, figures or axes. This step is OK. Here is an example figure:

Colorbar with the only 8 entries.

1

1 Answers

2
votes

You want to modify the Ticks and TickLabels property of the object created by colorbar:

For example:

CX = [0.539700, 0.000000, 0.000000; ...
1.000000, 0.460300, 0.000000; ...
1.000000, 0.571400, 0.000000; ...
0.460300, 1.000000, 0.539700; ...
0.428600, 1.000000, 0.571400; ...
0.000000, 0.920600, 1.000000; ...
0.000000, 0.285700, 1.000000; ...
0.000000, 0.190500, 1.000000; ...
0.000000, 0.190500, 1.000000; ...
0.000000, 0.047600, 1.000000; ...
0.000000, 0.000000, 0.968300; ...
0.000000, 0.000000, 0.968300; ... 
0.000000, 0.000000, 0.952400; ...
0.000000, 0.000000, 0.714300; ...
0.000000, 0.000000, 0.507900];

cmap = colormap(CX);
cbar = colorbar;

Gives us the following:

yay1

The range of the colorbar is dependent on the range of values in the axes object the colorbar is linked to. For a blank axes we get a colorbar range of [0, 1]. For something like surf(peaks) we get (roughly) [-6.5 8.08]. Ticks outside this range will not be rendered. With this in mind, for a blank axis we can do something like:

nticks = 15;
cbar.Ticks = 0:(1/nticks):1;

Which gives us:

yay2

And then we can modify the tick labels accordingly:

% Generate Fibonacci sequence, because why not
a = zeros(1, nticks + 1);
a(1) = 1; a(2) = 1;
ii = 3;
while ii <= nticks + 1
    a(ii) = a(ii-2) + a(ii-1);
    ii = ii + 1;
end

cbar.TickLabels = a;

Leaving us with:

yay3

Note, from the documentation for colorbar:

Tick mark labels, specified as a cell array of strings, a numeric array, or a string

So you're not limited to a numeric vector.


Edit: The above assumes R2014b or newer in order to use the dot notation to access properties of graphics objects.

For older versions of MATLAB, use set:

set(cbar, 'Ticks', 0:(1/nticks):1);
set(cbar, 'TickLabels', a);

Which produces the same results.