This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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.

c = colorbar;

Store the current positions of the axes and the colorbar.

Note: Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the get and set functions instead, such as axpos = get(ax,'Position').

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?