how to set the ticks in the colorbar to be automatic and as many as possible?
1 次查看(过去 30 天)
显示 更早的评论
i'm trying to use as many ticks in the colorbar doing so manualy lead to unexpected resault such as:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145240/image.png)
(in the attached image)
i used:
cbr=colorbar('fontsize',15);
set(cbr,'YTick',min(min(MAT)):max(max(MAT))/100:max(max(MAT)))
how can i still get as many ticks as possible but still keeping the data readable
1 个评论
Stephen23
2014-9-2
编辑:Stephen23
2014-9-2
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.
回答(1 个)
Image Analyst
2014-9-1
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.
2 个评论
Image Analyst
2014-9-2
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, []);
colormap(jet(256));
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);
uiwait(msgbox(message));
% Set fontsize in pixels
% set(hColorBar, 'FontSize', pixelsPerNumber);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!