MATLAB: How to reposition the colorbar to not overlap the x-axis label

MATLAB

When I create a plot with a horizontal colorbar my x-axis label is covered by the colorbar itself so that it is no longer visible. How can I reposition my colorbar so that the label is uncovered.
plot(1:10);
xlabel('\Omega_m');
ylabel('H_0');
hbar = colorbar('horiz');
xlabel(hbar,'\Omega_c h^2');

Best Answer

To ensure that overlap of the x-axis label and the colorbar does not occur, you can modify the position of the axes in the figure and the position of the colorbar. For example:
%Create Plot as before
plot(1:10);
xlabel('\Omega_m');
ylabel('H_0');
ax = gca;
hbar = colorbar('horiz');
xlabel(hbar,'\Omega_c h^2');
% Modify Colorbar to a manual setting
set(hbar,'location','manual','ActivePositionProperty','OuterPosition')
% Reset the outerposition of the colorbar to be normalized from figure
hbarPos = get(hbar,'OuterPosition');
set(hbar,'OuterPosition',[0 0 1 hbarPos(4)])
% What are the axes positions and margin info
axPos = get(ax,'Position');
axMargin = get(ax,'TightInset');
% Calculate and set new position of axes to accomodate colorbar and margins
newAxPos = [axPos(1),hbarPos(4)+axMargin(2),...
axPos(3), axPos(4)+axPos(2)-hbarPos(4)-axMargin(2)-axMargin(4)];
set(ax,'ActivePositionProperty','Position','Position',newAxPos)