Adjusting width of horizontal colorbar

112 vues (au cours des 30 derniers jours)
John Cruce
John Cruce le 9 Sep 2021
Commenté : John Cruce le 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?

Réponse acceptée

Chunru
Chunru le 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 commentaires
Chunru
Chunru le 10 Sep 2021
Modifié(e) : Chunru le 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 le 11 Sep 2021
I changed the figure size and that did the trick. Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by