How can I change the position of the numbers in colorbar?
9 次查看(过去 30 天)
显示 更早的评论
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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193069/image.png)
Cheers!
6 个评论
jonas
2018-8-6
编辑:jonas
2018-8-6
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?
回答(1 个)
jonas
2018-8-6
编辑:jonas
2018-8-7
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colorbar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!