Adjusting width of horizontal colorbar

143 visualizaciones (últimos 30 días)
John Cruce
John Cruce el 9 de Sept. de 2021
Comentada: John Cruce el 11 de Sept. de 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?

Respuesta aceptada

Chunru
Chunru el 9 de Sept. de 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 comentarios
Chunru
Chunru el 10 de Sept. de 2021
Editada: Chunru el 11 de Sept. de 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 el 11 de Sept. de 2021
I changed the figure size and that did the trick. Thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by