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
Control how the FaceColor
property is set, specified as one of these values:
"auto"
— MATLAB® controls the value of theFaceColor
property by using theSeriesIndex
property of theBar
object and theColorOrder
property of the axes."manual"
— You set the value of theFaceColor
property directly, or indirectly as a function argument when you create theBar
object.
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:
- One color for all bars — Set the
FaceColor
property to a color name, RGB triplet, or a hexadecimal color code. - Different colors for one or more bars — Set the
FaceColor
property to"flat"
. Then set theCData
property to an RGB triplet, matrix of RGB triplets, scalar colormap index, or a vector of colormap indices.
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:
- Set the
FaceColorMode
property to"auto"
. - Set the
FaceColor
property to"flat"
, and set theCDataMode
property to"auto"
.
In some cases, MATLAB sets the SeriesIndex
property to0
, which also disables automatic color selection.
Bar Labels
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" | ![]() |
"end-outside" | ![]() |
Since R2024b
Control how the LabelLocation
property is set, specified as one of these values:
"auto"
— MATLAB controls the value of theLabelLocation
property depending on whether the bars are grouped or stacked."manual"
— You set the value of theLabelLocation
property directly, and the location does not change.
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.
- An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range
[0,1]
, for example,[0.4 0.6 0.7]
. - A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (
#
) followed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Therefore, the color codes"#FF8800"
,"#ff8800"
,"#F80"
, and"#f80"
are equivalent.
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" | ![]() |
"green" | "g" | [0 1 0] | "#00FF00" | ![]() |
"blue" | "b" | [0 0 1] | "#0000FF" | ![]() |
"cyan" | "c" | [0 1 1] | "#00FFFF" | ![]() |
"magenta" | "m" | [1 0 1] | "#FF00FF" | ![]() |
"yellow" | "y" | [1 1 0] | "#FFFF00" | ![]() |
"black" | "k" | [0 0 0] | "#000000" | ![]() |
"white" | "w" | [1 1 1] | "#FFFFFF" | ![]() |
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._ | ![]() |
"glow" — Dark theme default | ![]() |
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:
"auto"
— MATLAB controls the value of theLabelColor
property by selecting a neutral contrasting color."manual"
— You set the value of theLabelColor
property directly, and the color does not change.
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
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.
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:
"auto"
— MATLAB selects theGroupWidth
value based on the number of groups."manual"
— You specify theGroupWidth
value.
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.
'on'
— Display bars horizontally. If you create a graph with barh, then theHorizontal
property is set to'on'
.'off'
— Display bars vertically. If you create the chart with bar, then theHorizontal
property is set to'off'
.
Data
Color data, specified as one of these values:
- RGB triplet — Single RGB color value applies to all bars.
- Three-column matrix — One color per bar. Each row in the matrix specifies an RGB triplet for a particular bar.
- Scalar — Single color applies to all bars, where the color comes from the colormap.
- Vector — One color per bar. The colors come from the colormap.
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];
Control how the CData
property is set, specified as one of these values:
"auto"
— MATLAB controls the value of theCData
property."manual"
— You set the value of theCData
property directly, or indirectly as a function argument when you create theBar
object.
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.
- For vertical bar charts, the values are the bar locations along the _x_-axis.
- For horizontal bar charts, the values are the bar locations along the _y_-axis.
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
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
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
This property is read-only.
Parent/Child
Parent, specified as an Axes
, Group
, or Transform
object.
Identifiers
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
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.