Bar - Bar chart appearance and behavior - MATLAB (original) (raw)

Bar Properties

Bar chart appearance and behavior

Bar properties control the appearance and behavior of a Bar object. By changing property values, you can modify certain aspects of the bar chart. Use dot notation to query and set properties.

b = bar(1:10); c = b.FaceColor b.FaceColor = [0 0.5 0.5];

Color and Styling

expand all

Control how the FaceColor property is set, specified as one of these values:

If you change the value of the FaceColor property manually, MATLAB changes the value of the FaceColorMode property to"manual".

Face transparency, specified as a scalar in the range[0,1]. A value of 1 is opaque and 0 is completely transparent. Values between 0 and 1 are semitransparent.

Example: b = bar(1:10,'FaceAlpha',0.5)

Example: b.FaceAlpha = 0.5;

Edge transparency, specified as a scalar in the range[0,1]. A value of 1 is opaque and 0 is completely transparent. Values between 0 and 1 are semitransparent.

Example: b = bar(1:10,'EdgeAlpha',0.5)

Example: b.EdgeAlpha = 0.5;

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Series index, specified as a positive whole number or"none". This property is useful for matching the colors of graphics objects, such as text, plot lines, or otherBar objects.

By default, the SeriesIndex property of aBar object is a number that corresponds to its order of creation, starting at 1. MATLAB uses the number to calculate an index for automatically assigning colors when you call plotting functions. The index refers to the rows of the array stored in the ColorOrder property of the axes. Any objects in the axes that have the sameSeriesIndex number will have the same color.

A SeriesIndex value of "none" corresponds to a neutral color that does not participate in the indexing scheme. (since R2023b)

How Manual Color Assignment Overrides SeriesIndex Behavior

To manually control the colors of the bars, use either of these approaches:

When you manually set the color of a Bar object, MATLAB disables automatic color selection for that object and allows your color to persist, regardless of the value of theSeriesIndex property. The mode properties,FaceColorMode andCDataMode, indicate whether the colors have been set manually (by you) or automatically. A value of"manual" indicates manual selection, and a value of "auto" indicates automatic selection.

To enable automatic selection again, set theSeriesIndex property to a positive whole number and perform either of these steps:

In some cases, MATLAB sets the SeriesIndex property to0, which also disables automatic color selection.

Bar Labels

expand all

Since R2024b

Bar labels, specified as a string vector, cell array of character vectors, numeric vector, datetime vector, duration vector, or categorical vector. The length of the vector must match the number of bars.

When the chart displays multiple series of grouped or stacked bars, each series corresponds to one Bar object. Set theLabels property for each Bar object that you want labeled.

Since R2024b

Location of bar labels, specified as "end-inside" or"end-outside". You might need to adjust the axes limits to provide enough space for the labels.

Value Example
"end-inside" Bar chart with the labels inside of the bars
"end-outside" Bar chart with the labels outside of the bars

Since R2024b

Control how the LabelLocation property is set, specified as one of these values:

If you change the value of the LabelLocation property manually, MATLAB changes the value of theLabelLocationMode property to"manual".

Since R2024b

Label color, specified as an RGB triplet, hexadecimal color code, color name, or short name. To use a different color for each label, specify an_m_-by-3 matrix of RGB triplets or a string vector of_m_ hexadecimal color codes, color names, or short names. The value m is the number of labels (one for each bar).

For a custom color, specify an RGB triplet or a hexadecimal color code.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"red" "r" [1 0 0] "#FF0000" Sample of the color red
"green" "g" [0 1 0] "#00FF00" Sample of the color green
"blue" "b" [0 0 1] "#0000FF" Sample of the color blue
"cyan" "c" [0 1 1] "#00FFFF" Sample of the color cyan
"magenta" "m" [1 0 1] "#FF00FF" Sample of the color magenta
"yellow" "y" [1 1 0] "#FFFF00" Sample of the color yellow
"black" "k" [0 0 0] "#000000" Sample of the color black
"white" "w" [1 1 1] "#FFFFFF" Sample of the color white

This table lists the default color palettes for plots in the light and dark themes.

Palette Palette Colors
"gem" — Light theme default_Before R2025a: Most plots use these colors by default._ Sample of the "gem" color palette
"glow" — Dark theme default Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem"); H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

Since R2024b

Control how the LabelColor property is set, specified as one of these values:

If you change the value of the LabelColor property manually, MATLAB changes the value of the LabelColorMode property to "manual".

Font size, specified as a scalar value greater than zero in point units. The default font size depends on the specific operating system and locale. One point equals 1/72 inch.

Bar Graph Type

expand all

Since R2024a

Width of the bar groups, specified as a scalar in the range [0, 1]. This property specifies the fraction of the available space for a group of bars. It has no effect if the bars are not grouped. A value of1 uses all of the available space for each group, but it minimizes the space between groups. Smaller values produce thinner bars with more space between the groups.

For example, these bar charts are the same except for theirGroupWidth values. As theGroupWidth value increases, the bars become wider and the groups become harder to distinguish.

Three bar charts that are the same except for their GroupWidth values

Plotting groups of bars produces multiple Bar objects. Changing the GroupWidth property of one object changes the value for all of the objects.

Since R2024a

Control how the GroupWidth property is set, specified as one of these values:

Horizontal bar chart, specified as 'on' or'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

Data

expand all

Color data, specified as one of these values:

By default, when you create a bar chart, the CData property contains a three-column matrix of RGB triplets. You can change the color for a particular bar by changing the corresponding row in the matrix.

This property applies only when the FaceColor orEdgeColor property is set to 'flat'.

Example

Change the color for a particular bar by setting the FaceColor property to 'flat'. Then change the corresponding row in theCData matrix to the new RGB triplet. For example, change the color of the second bar.

b = bar(1:10,'FaceColor','flat'); b.CData(2,:) = [0 0.8 0.8];

Bar chart that has all dark blue bars except the second bar, which is cyan.

Control how the CData property is set, specified as one of these values:

If you change the value of the CData property manually, MATLAB changes the value of the CDataMode property to "manual".

Bar locations, specified as a vector with no repeating values.

Alternatively, specify the bar locations using the input argumentX to the bar orbarh function. If you do not specifyX, then the indices of the values inYData determine the bar locations.

XData and YData must have equal lengths.

Example: 1:10

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

Bar lengths, specified as a vector. Alternatively, specify the bar lengths using the input argument Y to the bar or barh function.

XData and YData must have equal lengths.

Example: 1:10

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | duration

This property is read-only.

_x_-coordinates of the tips of the bars, returned as a vector. These coordinates are useful when you want to add text, error bars, or other objects to the tips of the bars. For example, you can pass the value of this property to the text function when you want to add text to the tips of the bars.

This property is read-only.

_y_-coordinates of the tips of the bars, returned as a vector. These coordinates are useful when you want to add text, error bars, or other objects to the tips of the bars. For example, you can pass the value of this property to the text function when you want to add text to the tips of the bars.

Legend

expand all

Legend label, specified as a character vector or string scalar. The legend does not display until you call the legend command. If you do not specify the text, then legend sets the label using the form'dataN'.

Interactivity

expand all

Data tip content, specified as a DataTipTemplate object. You can control the content that appears in a data tip by modifying the properties of the underlying DataTipTemplate object. For a list of properties, seeDataTipTemplate Properties.

For an example of modifying data tips, see Create Custom Data Tips.

Note

The DataTipTemplate object is not returned byfindobj or findall, and it is not copied by copyobj.

Callbacks

Callback Execution Control

expand all

This property is read-only.

Parent/Child

expand all

Parent, specified as an Axes, Group, or Transform object.

Identifiers

expand all

This property is read-only.

Type of graphics object, returned as 'bar'. Use this property to find all objects of a given type within a plotting hierarchy, such as searching for the type using findobj.

Object identifier, specified as a character vector or string scalar. You can specify a unique Tag value to serve as an identifier for an object. When you need access to the object elsewhere in your code, you can use the findobj function to search for the object based on the Tag value.

Version History

Introduced before R2006a

expand all

Control the bar group width by setting the GroupWidth property of at least one Bar object in the axes.

A value of 1 uses all of the available space for each group, but it minimizes the space between groups. Smaller values produce thinner bars with more space between the groups.

Opt out of automatic color selection for Bar objects by setting theSeriesIndex property to "none". When you specify"none", the Bar object has a neutral color.

To enable automatic color selection again, set the SeriesIndex property to a positive whole number.

The BaseValue property of a bar chart no longer depends on the axes limits. The property value stays the same when you change axes limits or pan within the axes.

Control how Bar objects vary in color by setting theSeriesIndex property. This property is useful when you want to match the colors of different objects in the axes.

Setting or getting UIContextMenu property is not recommended. Instead, use the ContextMenu property, which accepts the same type of input and behaves the same way as theUIContextMenu property.

There are no plans to remove the UIContextMenu property, but it is no longer listed when you call the set, get, orproperties functions on the Bar object.