How to get a consistent calibrated scale on a colourbar, despite changes in figure window size

1 view (last 30 days)
Hi,
When plotting data defined over two dimensions using 'image', I often want to put a colourbar in to make the meaning of the data apparent.
To take a usual example for me - I do time/frequency decompositions of time-varying signals from brain data. I've run some stats, and I have a t-value for each time point and each frequency bin. I represent these in a 2D frequencyXtime matrix, and plot them with the data normalized to within 0 and 64, so I can use 'image' directly.
I then try to add a colourbar to make clear the meaning of a given colour. Some example code:
>> % plotdata.datamatrix is the [freq X time] data
% if we want to represent everything from the lowest value to the highest value plotmatrix = plotdata.datamatrix;
plotmatrix = plotmatrix.*( 64 / (max(max(plotmatrix)) - min(min(plotmatrix))) ) + 32; % rescale to fill the colour range
maxColourVal = max(max(plotdata.datamatrix)); % take a note of the data values at the limmits of the range minColourVal = min(min(plotdata.datamatrix));
% plot! image(plotmatrix);
% make a calibrated colorbar colourTicks = [0,32,64]; colourTickLabels = linspace(minColourVal,maxColourVal,3); colourTickLabels = (1/100)*(round(colourTickLabels.*100)); colorbar('YTick', colourTicks); colorbar('YTickLabel', colourTickLabels);
>>
When I do this, I do get a colorbar with new labels - but - the labels depend on the size of the figure on the screen. Maximise the plot, and the number of labels and their location on the colorbar changes. For example, above I only have three labels - the minimum, centre and top of the colorbar. This is fine for a small figure, but when I maximise it, the labels actually repeat in a loop.
Does anyone know of a way to avoid this behaviour?
Thanks! George

Answers (1)

Sean de Wolski
Sean de Wolski on 1 May 2012
Manually set the colorbar's units property to 'pixels' and position to where you want it in pixels.
E.g:
h = colorbar;
set(h,'units','pixels','position',[0 0 50 250])

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!