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

조회 수: 5 (최근 30일)
yogan sganzerla
yogan sganzerla 2018년 8월 2일
편집: jonas 2018년 8월 7일
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
jonas
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?
Adam Danz
Adam Danz 2018년 8월 6일
편집: Adam Danz 2018년 8월 6일
...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.

댓글을 달려면 로그인하십시오.

답변 (1개)

jonas
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)

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by