Adjusting width of horizontal colorbar

112 Ansichten (letzte 30 Tage)
John Cruce
John Cruce am 9 Sep. 2021
Kommentiert: John Cruce am 11 Sep. 2021
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?

Akzeptierte Antwort

Chunru
Chunru am 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 Kommentare
Chunru
Chunru am 10 Sep. 2021
Bearbeitet: Chunru am 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 am 11 Sep. 2021
I changed the figure size and that did the trick. Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by