MATLAB Answers


how to set the ticks in the colorbar to be automatic and as many as possible?

Asked by Kobi
on 1 Sep 2014
Latest activity Edited by Stephen Cobeldick on 2 Sep 2014
i'm trying to use as many ticks in the colorbar doing so manualy lead to unexpected resault such as:
(in the attached image)
i used:
how can i still get as many ticks as possible but still keeping the data readable

  1 Comment

If you are wanting to maximize the the number of tickmarks but keep them distinct, then number does not depend on the data range, but instead on the height of the colorbar and text fontsize (as Image Analyst described below).
To calculate this automatically take a look at the axes and text properties, as well as the commands get and set . It might also be useful to look at the default text fontsize.
If an automatic calculation is unjustifiably too much work, or is too intimidating, then you will have to set it manually.

Sign in to comment.


1 Answer

Answer by Image Analyst
on 1 Sep 2014

I thing you might have just do trial and error. It depends on the font size and how many pixels are between the top of the bar and the bottom. Maybe if you can figure out the height of the colorbar in pixels and specify the fontsize in pixels, you might be able to calculate it. I don't have any code all ready to hand you though.


maybe help me figure out how to improve my idea? update: i use:
the only problem is if the input matrix is complex real+imaginary only the real part is shown on the colorbar ticks is there any way to fix this?
I don't know. I can't experiment with it anymore - gotta do something else now. Here is what I got so far. You can take over now.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
z = peaks(1000);
imshow(z, []);
hColorBar = colorbar('Units', 'pixels', 'FontSize', 35);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
position = get(hColorBar, 'Position')
% Get the tick numbers
allProperties = get(hColorBar) % Print out everything you can get to the command window.
tickLabels = get(hColorBar, 'YTickLabel')
tickNumbers = get(hColorBar, 'YTick')
numberOfLabels = length(tickLabels);
pixelsPerNumber = position(4) / numberOfLabels
message = sprintf('Your colorbar is %.1f pixels high\nand there are %d labels.\nYou can have %.1f vertical pixels per color bar tick label', ...
position(4), numberOfLabels, pixelsPerNumber);
% Set fontsize in pixels
% set(hColorBar, 'FontSize', pixelsPerNumber);

Sign in to comment.