Move colorbar closer to figure?
45 views (last 30 days)
Show older comments
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;
0 Comments
Answers (1)
Voss
on 15 Mar 2024
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 Comments
Voss
on 18 Mar 2024
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
on 18 Mar 2024
Note that I changed the colorbar position adjustment from 0.03 to 0.01 for the tall figure. Colorbar units are 'normalized' by default.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!