Change Colorbar Width

This example shows how to change the width of the colorbar by setting its Position property. The Position property sets the location and size of the colorbar. Specify the Position property value as a four-element vector of the form [left,bottom,width,height], where the first two elements set the colorbar position relative to the figure, and the last two elements set its size.

Create a checkerboard plot of the peaks function and add a colorbar.

figure
pcolor(peaks)
c = colorbar;

Store the current positions of the axes and the colorbar.

ax = gca;
axpos = ax.Position;
cpos = c.Position;

Change the colorbar width to half of its original width by adjusting the third element in cpos. Set the colorbar Position property to the updated position vector. Then, reset the axes to its original position so that it does not overlap the colorbar.

cpos(3) = 0.5*cpos(3);
c.Position = cpos;
ax.Position = axpos;

The graph displays a colorbar that has a narrow width.

See Also

|

Was this topic helpful?