Move colorbar closer to figure?

조회 수: 46 (최근 30일)
John Cruce
John Cruce 2024년 3월 15일
댓글: Voss 2024년 3월 18일
I have a colorbar with no title that I need to move closer to my figure to reduce padding/white space between the figure and the colorbar. I've tried to experiment with position using the colorbar position properties but I'm striking out. Below is a snippet of code:
How can I adjust the colorbar toward the figure (in this case from bottom toward the top)?
hcb=colorbar('SouthOutside');
% Set the number of colorbar ticks and length of tick marks
set(hcb,'YTick',[-6:0.25:6],'TickLength',[0.01]);
% Label tick marks but only label every other tick mark
set(hcb,'YTickLabel',{'-6';'';'';'';'-5';'';'';'';'-4';'';'';'';'-3';'';'';'';'-2';'';'';'';'-1';'';'';'';'0';'';'';'';'1';'';'';'';'2';'';'';'';'3';'';'';'';'4';'';'';'';'5';'';'';'';'6'});
hcb.Ruler.TickLabelRotation=0;

답변 (1개)

Voss
Voss 2024년 3월 15일
hcb=colorbar('SouthOutside');
% Set the number of colorbar ticks and length of tick marks
set(hcb,'YTick',[-6:0.25:6],'TickLength',[0.01]);
% Label tick marks but only label every other tick mark
set(hcb,'YTickLabel',{'-6';'';'';'';'-5';'';'';'';'-4';'';'';'';'-3';'';'';'';'-2';'';'';'';'-1';'';'';'';'0';'';'';'';'1';'';'';'';'2';'';'';'';'3';'';'';'';'4';'';'';'';'5';'';'';'';'6'});
hcb.Ruler.TickLabelRotation=0;
% move colorbar up slightly:
drawnow
hcb.Position(2) = hcb.Position(2)+0.01;
  댓글 수: 7
Voss
Voss 2024년 3월 18일
What figure properties are you setting? Only Position?
figure('position', [0, 0, 730, 1300]);
caxis([-6 6]); % R2019b
hcb=colorbar('SouthOutside');
% Set the number of colorbar ticks and length of tick marks
set(hcb,'YTick',[-6:0.25:6],'TickLength',[0.01]);
% Label tick marks but only label every other tick mark
set(hcb,'YTickLabel',{'-6';'';'';'';'-5';'';'';'';'-4';'';'';'';'-3';'';'';'';'-2';'';'';'';'-1';'';'';'';'0';'';'';'';'1';'';'';'';'2';'';'';'';'3';'';'';'';'4';'';'';'';'5';'';'';'';'6'});
hcb.Ruler.TickLabelRotation=0;
% store axes Position:
axPos = get(gca,'Position');
% move colorbar up slightly:
hcb.Position(2) = hcb.Position(2)+0.01; % back to 0.01 for a figure of this height
% restore axes Position:
set(gca,'Position',axPos);
Voss
Voss 2024년 3월 18일
Note that I changed the colorbar position adjustment from 0.03 to 0.01 for the tall figure. Colorbar units are 'normalized' by default.

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

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by