how to set the ticks in the colorbar to be automatic and as many as possible?
조회 수: 3 (최근 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에서 Axis Labels에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!