Move colorbar closer to figure?
28 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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
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
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 Center 和 File Exchange 中查找有关 Color and Styling 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!