Main Content

lcolorbar

Color bar with text labels

    Description

    lcolorbar(labels) displays a vertical color bar, with labels specified by the text in labels, to the right of the current axes.

    lcolorbar(labels,Name,Value) specifies options for the color bar using name-value arguments.

    h = lcolorbar(___) returns the color bar, using any combination of input arguments from the previous syntaxes.

    example

    Examples

    collapse all

    Import elevation data for a region surrounding South Boulder Peak in Colorado. Calculate the viewshed for a sample point 1000 meters above the terrain.

    [Z,R] = readgeoraster("n39_w106_3arc_v2.dt1","OutputType","double");
    [vis,visR] = viewshed(Z,R,39.6,-105.6,1000);

    Display the viewshed on a map, using green for visible areas and purple for obscured areas.

    figure
    usamap(vis,visR)
    geoshow(vis,visR,"DisplayType","surface")
    cmap = [0.4940 0.1840 0.5560; 0.4660 0.6740 0.1880];

    Add a labeled color bar. Avoid truncating the text by moving the color bar to the left.

    colormap(cmap)
    h = lcolorbar(["Obscured" "Visible"]);
    h.Position(1) = h.Position(1)-0.1;

    Figure contains 2 axes objects. Axes object 1 contains an object of type image. Hidden axes object 2 contains 10 objects of type patch, surface, line, text.

    Input Arguments

    collapse all

    Tick mark labels, specified as a cell array of character vectors or a string array. The number of elements in labels must match the length of the colormap.

    Example: ["blue" "green" "yellow"]

    Data Types: cell | char | string

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: lcolorbar(labels,Location="horizontal") displays a horizontal color bar below the axes.

    Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

    Example: lcolorbar(labels,"Location","horizontal") displays a horizontal color bar below the axes.

    Note

    In addition to the name-value arguments in this section, you can use name-value arguments to specify values for the properties of the Axes object returned by this function. For more information about the properties of Axes objects, see Axes Properties.

    Location of the color bar with respect to the axes, specified as one of these options:

    • "vertical" — Display the color bar vertically and to the right of the axes.

    • "horizontal" — Display the color bar horizontally and below the axes.

    Data Types: char | string

    Title of the color bar, specified as a character vector or string scalar.

    Data Types: char | string

    x-label of the color bar, specified as a character vector or string scalar.

    Data Types: char | string

    y-label of the color bar, specified as a character vector or string scalar.

    Data Types: char | string

    z-label of the color bar, specified as a character vector or string scalar.

    Data Types: char | string

    Alignment of the labels in the color bar, specified as one of these options:

    • "center" — Center the labels on the color bands.

    • "ends" — Center the labels on the color breaks.

    Data Types: char | string

    Output Arguments

    collapse all

    Output color bar, returned as an Axes object.

    Limitations

    In some cases, such as when adding color bars to subplots or when changing the colormap of a specified axes, the lcolorbar function produces unexpected results. In these cases, create a labeled color bar by using the colorbar function instead of the lcolorbar function.

    This code shows how to make a labeled color bar with 5 labels by using the colorbar function. Within the code:

    • Create an axes object, change the colormap, and add a color bar

    • Place a tick mark at the center of each color band

    • Add the tick labels

    ax = axes;
    n = 5;
    colormap(ax,jet(n))
    cb = colorbar;
    
    ticks = linspace(ax.CLim(1),ax.CLim(2),1 + 2*n);
    ticks(1:2:end) = [];
    cb.Ticks = ticks;
    
    labels = ["apples","oranges","grapes","peaches","melons"];
    cb.TickLabels = labels;
    

    Version History

    Introduced before R2006a

    See Also

    Functions