Adjusting width of horizontal colorbar

조회 수: 145 (최근 30일)
John Cruce
John Cruce 2021년 9월 9일
댓글: John Cruce 2021년 9월 11일
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?

채택된 답변

Chunru
Chunru 2021년 9월 9일
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
Chunru
Chunru 2021년 9월 10일
편집: Chunru 2021년 9월 11일
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 2021년 9월 11일
I changed the figure size and that did the trick. Thank you.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by