Adjusting width of horizontal colorbar

112 views (last 30 days)
I have a horizontal colorbar that I want to set the width and keep centered under my figure. I've attempted to change the width element of the colorbar position attribute similar to below:
hcb=colorbar('SouthOutside');
x1=get(gca,'position');
colorbarpos=hcb.Position;
colorbarpos(4)=colorbarpos(4)*0.9;
set(hcb,'Position',colorbarpos);
set(ax,'position',x1);
While I'm able to shrink the height, oddly the width of the colorbar increases under the figure. Does anyone know how I might simply decrease the height of the horizontal colorbar while keeping the colorbar the same width as the figure above?

Accepted Answer

Chunru
Chunru on 9 Sep 2021
ax1 = gca;
imagesc(peaks(80))
hcb=colorbar('SouthOutside');
ax1Pos = ax1.Position;
pos = hcb.Position;
pos(4) = 0.5*pos(4);
hcb.Position = pos;
% The above automatically change the ax1.Position
% We restore the origninal ax1.Position
ax1.Position = ax1Pos;
% x1=get(gca,'position');
% colorbarpos=hcb.Position;
% colorbarpos(4)=colorbarpos(4)*0.9;
% set(hcb,'Position',colorbarpos);
%set(ax,'position',x1);
  3 Comments
Chunru
Chunru on 10 Sep 2021
Edited: Chunru on 11 Sep 2021
It might be caused by the custom functions m_proj and m_grid. Without them, everything works fine. You can try to move these two lines before "ax=gca;"
set(gcf,'units','pixel','position',[0,0,730,300],'papersize',[730,300],'color','w');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','w');
%m_proj('Miller Cylindrical','lon',[-92 -88],'lat',[28 32.5]);
ax=gca;
hcb=colorbar('SouthOutside');
hcb.Label.String='Test Color Bar';
axPos = ax.Position;
colorbarpos=hcb.Position;
colorbarpos(4)=0.4*colorbarpos(4);
hcb.Position = colorbarpos;
ax.Position = axPos;
%m_grid('xtick',[],'ytick',[],'linestyle','none','backgroundcolor',[0.61961 0.73333 0.84314]);
John Cruce
John Cruce on 11 Sep 2021
I changed the figure size and that did the trick. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!