Accelerating the pace of engineering and science

# bar

## Description

example

bar(y) creates a bar graph with one bar for each element in y. If y is a matrix, then bar groups the bars according to the rows in y.

example

bar(x,y) draws the bars at the locations specified by x.

example

bar(___,width) sets the relative bar width, which controls the separation of bars within a group. Specify width as a scalar value. Use this option with any of the input argument combinations in the previous syntaxes.

example

bar(___,style) specifies the style of the bar groups. For example, use 'stacked' to display each group as one multicolored bar.

example

bar(___,color) sets the color for all the bars. For example, use 'r' for red bars.

example

bar(___,Name,Value) sets bar series properties using one or more Name,Value pair arguments. The settings apply to all bars plotted. For example, use 'EdgeColor','black' to outline all the bars in black.

example

bar(ax,___) plots into the axes specified by ax instead of into the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.

example

b = bar(___) returns one or more bar series objects. If y is a vector, then bar creates one bar series object. If y is a matrix, then bar creates a bar series object for each column. Use b to make future modifications to the bars after they are created.

## Examples

expand all

### Create Bar Graph

```y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y)
```

### Specify Bar Locations

Specify the bar locations along the x-axis.

```x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y)
```

### Specify Bar Width

Set the width of each bar to 40 percent of the total space available for each bar.

```y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,0.4)
```

### Display Groups of Bars

Display four groups of three bars.

```y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)
```

### Display Stacked Bars

Display one bar for each row of the matrix. The height of each bar is the sum of the elements in the row.

```y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked')
```

### Specify Subplot for Bar Graph

Create a figure with two subplots. In the upper subplot, plot a bar graph. In the lower subplot, plot a stacked bar graph of the same data.

```y = [1 2 3; 4 5 6];
ax1 = subplot(2,1,1);
bar(ax1,y)

ax2 = subplot(2,1,2);
bar(ax2,y,'stacked')
```

### Specify Bar Color

Create a bar graph using red bars.

```y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'r')
```

### Specify Bar and Outline Colors

Set the bar interior color and outline color using RGB triplets. Set the width of the bar outline.

```y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
```

### Change Properties for Specific Bar Series

Create a bar graph with a three-column matrix input and return the three bar series objects. bar creates one bar series for each column in the matrix.

```y = [2 4 6; 3 4 5];
b = bar(y);
```

Change properties for a specific bar series by indexing into the object array. For example, change properties of the bars representing the second column of y using b(2). Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

```b(2).LineWidth = 2;
b(2).EdgeColor = 'red';
```

## Input Arguments

expand all

### x — x valuesvector | matrix

x values, specified as a vector or a matrix. If x and y are both vectors, then they must be equal length. If x and y are both matrices, then they must be equal size. If x is a vector and y is a matrix, then the length of x must equal the number of rows in y.

The x values do not have to be in order, but they cannot contain duplicate values. If x is a matrix, then it cannot contain duplicate values across columns.

Example: 1:10

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

### y — y valuesvector | matrix

y values, specified as a vector or a matrix.

• If y is a vector, then bar draws one bar for each element. The bar function treats all vectors as column vectors.

• If y is a matrix, then bar groups the bars according to the rows in y.

Example: [10 8 5 7 3 9 1]

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

### width — Bar width0.8 (default) | scalar

Bar width, specified as a fraction of the total space available for each bar. The default of 0.8 means the bar width is 80% of the space from the previous bar to the next bar, with 10% of that space on each side.

If the width is 1, then the bars within a group touch one another.

Example: 0.5

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

### style — Bar group style'grouped' (default) | 'stacked' | 'hist' | 'histc'

Bar group style, specified by one of these values.

StylePurpose

'grouped'

Display one group for each row in y.

• If y is an m-by-n matrix, then bar displays m groups of n vertical bars, where m is the number of rows and n is the number of columns in y.

• If y is a vector of length n, then bar displays one group of n bars. The bar function treats all vectors as column vectors.

'stacked'

Display one bar for each row in y.

• If y is an m-by-n matrix, then bar displays m bars where each bar height is the sum of the elements in the row. Each bar is multicolored. Colors correspond to distinct elements and show the relative contribution each row element makes to the total sum.

• If y is a vector of length n, then bar displays n bars. The bar function treats all vectors as column vectors.

'histc'

Display the graph in histogram format, in which bars touch one another.

'hist'

Display the graph in histogram format, but center each bar over the x-ticks, rather than making bars span x-ticks as the histc option does.

### color — Bar color'b' | 'r' | 'g' | 'c' | 'm' | 'y' | 'k' | 'w'

Bar color, specified as one of the strings in this table.

StringColor
'b'Blue
'r'Red
'g'Green
'c'Cyan
'm'Magenta
'y'Yellow
'k'Black
'w'White

### ax — Axes objectaxes object

Axes object. If you do not specify an axes, then bar uses the current axes for the bar graph.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'EdgeColor','g' specifies a green outline around the bars.

The bar series properties listed here are only a subset. For a complete list, see Bar Series Properties.

 Note:   You cannot specify Name,Value pairs when using the 'hist' or 'histc' bar group style options.

### 'EdgeColor' — Bar outline color[0 0 0] (default) | 'flat' | 'none' | RGB triplet | color string

Bar outline color, specified as one of these values:

• 'flat' — Colors based on axes colormap. To change the colormap, use the colormap function.

• 'none' — No color, which makes the outlines invisible.

• RGB triplet or color string — Specify a custom color. The default RGB triplet value of [0 0 0] corresponds to black.

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]. This table lists RGB triplet values that have equivalent color strings.

Long NameShort NameRGB Triplet
'yellow''y'[1 1 0]
'magenta''m'[1 0 1]
'cyan''c'[0 1 1]
'red''r'[1 0 0]
'green''g'[0 1 0]
'blue''b'[0 0 1]
'white''w'[1 1 1]
'black'k'[0 0 0]

Example: 'blue'

Example: [0 0 1]

### 'FaceColor' — Bar fill color'flat' (default) | 'none' | RGB triplet | color string

Bar fill color, specified as one of these values:

• 'flat' — Colors based on the axes colormap.

• 'none' — No color, which makes the fill invisible.

• RGB triplet or color string — Specify a custom color.

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]. This table lists RGB triplet values that have equivalent color strings.

Long NameShort NameRGB Triplet
'yellow''y'[1 1 0]
'magenta''m'[1 0 1]
'cyan''c'[0 1 1]
'red''r'[1 0 0]
'green''g'[0 1 0]
'blue''b'[0 0 1]
'white''w'[1 1 1]
'black'k'[0 0 0]

### 'BaseValue' — Baseline location0 (default) | numeric scalar value

Baseline location, specified as a numeric scalar value.

### 'LineStyle' — Line style of bar outlines'-' (default) | '--' | ':' | '-.' | 'none'

Line style of bar outlines, specified as one of the strings in this table.

StringLine StyleResulting Line
'-'Solid line

'--'Dashed line

':'Dotted line

'-.'Dash-dotted line

'none'No lineNo line

### 'LineWidth' — Width of bar outlines0.5 (default) | positive value

Width of bar outlines, specified as a positive value in point units. One point equals 1/72 inch.

Example: 1.5

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

## Output Arguments

expand all

### b — Bar series objectssingle object | array of objects

Bar series objects, returned as a single object or an array of objects. Each bar series object comprises a set of bars that have the same color. Use the elements in b to access and modify properties of a specific bar series object after it has been created.

expand all

### Tips

• The default bar colors are based on the axes colormap. You can change the axes colormap using the colormap function.