Main Content

Creating Colorbars

Colorbars allow you to see the relationship between your data and the colors displayed in your chart. After you have created a colorbar, you can customize different aspects of its appearance, such as its location, thickness, and tick labels. For example, this colorbar shows the relationship between the values of the peaks function and the colors shown in the plot next to it.

contourf(peaks)
c = colorbar;

Figure contains an axes object. The axes object contains an object of type contour.

The default location of the colorbar is on the right side of the axes. However, you can move the colorbar to a different location by setting the Location property. In this case, the 'southoutside' option places the colorbar below the axes.

c.Location = 'southoutside';

Figure contains an axes object. The axes object contains an object of type contour.

You can also change the thickness of the colorbar. The Position property controls the location and size of most graphics objects, including axes and colorbars. Because this colorbar is horizontal, the fourth value in c.Position (which corresponds to height) controls its thickness. Here, the colorbar is narrowed and the axes position is reset so that there is no overlap with the colorbar.

ax = gca;
axpos = ax.Position;
c.Position(4) = 0.5*c.Position(4);
ax.Position = axpos;

Figure contains an axes object. The axes object contains an object of type contour.

Colorbar objects have several properties for modifying the tick spacing and labels. For example, you can specify that the ticks occur in only three places: -6.5, 0, and 8.

c.Ticks = [-6.5 0 8];

Figure contains an axes object. The axes object contains an object of type contour.

You can change the tick labels to any values. Use a cell array to specify the tick labels.

c.TickLabels = {'Frigid','Freezing','Cold'};

Figure contains an axes object. The axes object contains an object of type contour.

You can also use TeX or LaTeX markup. Use the TickLabelInterpreter property to set the interpreter when you use TeX or LaTeX.

c.TickLabelInterpreter = 'tex';
c.TickLabels = {'-6.5\circ','0\circ','8\circ'};

Figure contains an axes object. The axes object contains an object of type contour.

You can change the limits of the colorbar to focus on a specific region of color. For example, you can narrow the limits and adjust the tick labels to reflect the new limits. The resulting colorbar excludes the dark blue shades that used to be on the left and the yellow shades that used to be on the right.

c.Limits = [-4 4];
c.Ticks = [-4 0 4];
c.TickLabels = {'-4\circ','0\circ','4\circ'};

Figure contains an axes object. The axes object contains an object of type contour.

Add a descriptive label to the colorbar using the Label property. Because the Label property must be specified as a Text object, you must set the String property of the Text object first. Then you can assign that Text object to the Label property. The following command accomplishes both tasks in one step.

c.Label.String = 'Degrees Celsius';

Figure contains an axes object. The axes object contains an object of type contour.

See Also

Functions

Properties