How can I change the position of the numbers in colorbar?

39 views (last 30 days)
Dear, I would like to define the position of the number in that I have in colorbar.
To explain better, I am adding an image to show what I have and what I would like to have. This is the image.
Be attention on the 300ºC!
Cheers!
  6 Comments
jonas
jonas on 6 Aug 2018
Edited: jonas on 6 Aug 2018
Correct me if I am wrong, but I believe you have actually rotated this image before uploading. In reality you want a horizontal colorbar with vertical ticklabels, yes?
As Adam Danz pointed out, the default text orientation on colorbars is always horizontal, but you want vertical right?
Adam Danz
Adam Danz on 6 Aug 2018
Edited: Adam Danz on 6 Aug 2018
...or do you want a vertical colorbar with horizontal ticks? That one's easy:
h=colorbar('southoutside');
I don't know of a way to rotate the tick marks of a colorbar.

Sign in to comment.

Answers (1)

jonas
jonas on 6 Aug 2018
Edited: jonas on 7 Aug 2018
Rotating the ticklabels are surprisingly difficult. Ticklabels can not be rotated, but they can be replaced by normal text which in turn can be rotated. Here is a function to rotate the ticklabels of any colorbars with linear scale. Report back to me if you find any bugs. The ticklabels will always be on the right side (vertical) or below (horizontal) the colorbar.
function CTixRotate(cb)
cb.TickLabels={}
%%Grab some position properties for later use
ax_pos=get(gca,'position')
ax_limx=get(gca,'XLim')
ax_limy=get(gca,'YLim')
cb_pos=get(cb,'position')
cb_lim=get(cb,'limits')
%%Grab colorbar ticks
tix=cb.Ticks;
%%Create cell array with ticklabels
tixlabels=sprintfc('%.2g',tix)'
%%Normalized coordinates for ticks
%%Vertical colorbar
if cb_pos(4)>cb_pos(3)
ny=linspace(cb_pos(2),cb_pos(2)+cb_pos(4),numel(tix))';
nx=repmat(cb_pos(1)+cb_pos(3),numel(tix),1)
%%Horizontal colorbar
else
nx=linspace(cb_pos(1),cb_pos(1)+cb_pos(3),numel(tix))';
ny=repmat(cb_pos(2),numel(tix),1)
end
%%Convert from normalized (nx,ny) to axes (x,y) coordinates
ky=(diff(ax_limy)/((ax_pos(2)+ax_pos(4))-ax_pos(2)));
kx=(diff(ax_limx)/((ax_pos(1)+ax_pos(3))-ax_pos(1)));
y=ky.*ny-ky*ax_pos(2)+ax_limy(1);
x=kx.*nx-kx*ax_pos(1)+ax_limx(1);
%%Write out ticklabels
if cb_pos(4)>cb_pos(3)
ht=text(x,y,tixlabels',...
'rotation',-90,'verticalalignment','bottom','horizontalalignment','center')
else
ht=text(x,y,tixlabels',...
'rotation',-90,'verticalalignment','mid','horizontalalignment','left')
end
end
Example:
cb=colorbar;
cb.Position=[.5 .3 .05 .6];
caxis([5 10])
axis([2 7 90 100])
CTixRotate(cb)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!