Accelerating the pace of engineering and science

• Trials

# bar3

Plot 3-D bar graph

## Syntax

bar3(Y)
bar3(x,Y)
bar3(...,width)
bar3(...,'style')
bar3(...,LineSpec)
bar3(axes_handle,...)
h = bar3(...)

## Description

bar3 draws a three-dimensional bar graph.

bar3(Y) draws a three-dimensional bar chart, where each element in Y corresponds to one bar. When Y is a vector, the x-axis scale ranges from 1 to length(Y). When Y is a matrix, the x-axis scale ranges from 1 to size(Y,1) and the elements in each row are grouped together.

bar3(x,Y) draws a bar chart of the elements in Y at the locations specified in x, where x is a vector defining the y-axis intervals for vertical bars. The x-values can be nonmonotonic, but cannot contain duplicate values. If Y is a matrix, bar3 clusters elements from the same row in Y at locations corresponding to an element in x. Values of elements in each row are grouped together.

bar3(...,width) sets the width of the bars and controls the separation of bars within a group. The default width is 0.8, so if you do not specify x, bars within a group have a slight separation. If width is 1, the bars within a group touch one another.

bar3(...,'style') specifies the style of the bars. 'style' is 'detached', 'grouped', or 'stacked'. Default mode of display is 'detached'.

• 'detached' displays the elements of each row in Y as separate blocks behind one another in the x direction.

• 'grouped' displays n groups of m vertical bars, where n is the number of rows and m is the number of columns in Y. The group contains one bar per column in Y.

• 'stacked' displays one bar for each row in Y. The bar height is the sum of the elements in the row. Each bar is multicolored, with colors corresponding to distinct elements and showing the relative contribution each row element makes to the total sum.

bar3(...,LineSpec) displays all bars using the color specified by LineSpec.

bar3(axes_handle,...) plots into the axes with the handle axes_handle instead of into the current axes (gca).

h = bar3(...) returns a vector of handles to surface objects, one for each created. When Y is a matrix, bar3 creates one surface object per column in Y.

## Examples

expand all

### Create 3-D Bar Graph

Load the data set count.dat, which returns a three-column matrix, count. Store Y as the first 10 rows of count.

```load count.dat
Y = count(1:10,:);
```

Create a 3-D bar graph of Y. By default, the style is detached.

```figure
bar3(Y)
title('Detached Style')
```

### Specify Bar Width for 3-D Bar Graph

Load the data set count.dat, which returns a three-column matrix, count. Store Y as the first 10 rows of count.

```load count.dat
Y = count(1:10,:);
```

Create a 3-D bar graph of Y and set the bar width to 0.5.

```width = 0.5;

figure
bar3(Y,width)
title('Bar Width of 0.5')
```

### 3-D Bar Graph with Grouped Style

Load the data set count.dat, which returns a three-column matrix, count. Store Y as the first 10 rows of count.

```load count.dat
Y = count(1:10,:);
```

Create a 3-D bar graph of Y. Group the elements in each row of Y by specifying the style option as grouped.

```figure
bar3(Y,'grouped')
title('Grouped Style')
```

### 3-D Bar Graph with Stacked Style

Load the data set count.dat, which returns a three-column matrix, count. Store Y as the first 10 rows of count.

```load count.dat
Y = count(1:10,:);
```

Create a 3-D bar graph of Y. Stack the elements in each row of Y by specifying the style option as stacked.

```figure
bar3(Y,'stacked')
title('Stacked Style')
```