Documentation

This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

barh

Plot bar graph horizontally

Syntax

```barh(y) barh(x,y) barh(...,width) barh(...,style) barh(...,'color') barh(...,'PropertyName',PropertyValue,...) barh(ax,...) b = barh(...) ```

Description

A `barh` graph displays the values in a vector or matrix as horizontal bars.

`barh(y)` draws one horizontal bar for each element in `y`. If `y` is a matrix, `barh` groups the bars produced by the elements in each row. The y-axis scale ranges from 1 up to `length(y)` when `y` is a vector, and `1` to `size(y,1)`, which is the number of rows, when `y` is a matrix. The values in `y` can be numeric or duration values.

`barh(x,y)` draws a bar for each element in `y` at locations specified in `x`, where `x` is a vector defining locations along the y-axis. The location values can be nonmonotonic, but cannot contain duplicate values. If `y` is a matrix, `barh` groups the elements of each row in `y` at corresponding locations in `x`. The values in `x` can be numeric, datetime, duration, or categorical values.

`barh(...,width)` sets the relative bar width and controls the separation of bars within a group. The default `width` is `0.8`, so if you do not specify `x`, the bars within a group have a slight separation. If `width` is `1`, the bars within a group touch one another. The value of `width` must be a scalar.

`barh(...,style)` specifies the style of the bars. Specify `style` as one of these values:

• `'grouped'` displays m groups of n bars, where m is the number of rows and n is the number of columns in `y`. Each group contains one bar per column in `y`. This is the default value.

• `'stacked'` displays one bar for each row in `y`. The bar length 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. The `barh` function treats all vectors as column vectors. If `y` is a vector of length n, then `barh` displays n bars.

• `'histc'` displays the graph in histogram format, in which bars touch one another.

• `'hist'` also displays the graph in histogram format, but centers each bar over the tick value, rather than making bars span the tick values as the `histc` option does.

Note

When you use either the `hist` or `histc` option, you cannot also use parameter/value syntax. These two options create `Patch` objects rather than `Bar` objects.

`barh(...,'color')` displays all bars using the color specified by the single-letter abbreviation `'r'`, `'g'`, `'b'`, `'c'`, `'m'`, `'y'`, `'k'`, or `'w'`.

`barh(...,'PropertyName',PropertyValue,...)` sets the named property or properties to the specified values. You cannot specify properties when `hist` or `histc` options are used. See Bar Properties for more information.

`barh(ax,...)` plots into the axes `ax` instead of into the current axes (`gca`).

`b = barh(...)` returns a vector of `Bar` objects. When `y` is a matrix, `barh` creates one `Bar` object per column in `y`. Each `Bar` object comprises a set of bars that have the same color. Use `b` to change properties for all bars in a `Bar` object.

Examples

collapse all

Create a horizontal bar graph of vector data.

```y = [57,91,105,123,131,150,... 170,203,226.5,249,281.4]; figure barh(y)```

`barh` draws one horizontal bar for each element in `y`.

Specify the bar width to 0.4.

```y = [57,91,105,123,131,150,... 170,203,226.5,249,281.4]; figure; width = 0.4; barh(y,width);```

Create a figure with four subplots. In each subplot, create a horizontal bar graph using a different style option for each graph.

```x = [2,4]; y = [1,2,3,4;... 5,6,7,8]; figure; subplot(2,2,1); barh(x,y,'grouped'); % groups by row title('Grouped Style') subplot(2,2,2); barh(x,y,'stacked'); % stacks values in each row together title('Stacked Style') subplot(2,2,3); barh(x,y,'hist'); % centers bars over x values title('hist Style') subplot(2,2,4); barh(x,y,'histc'); % spans bars over x values title('histc Style')```

Create a horizontal bar graph and change the color of the bars to red.

```y = [57,91,105,123,131,150,... 170,203,226.5,249,281.4]; figure barh(y,'r')```

Create a horizontal bar graph with categorical values along the y-axis. By default, the categories display in alphabetical order.

```c = categorical({'apples','pears','oranges'}); prices = [1.23 0.99 2.3]; barh(c,prices)```

Create a horizontal bar graph and set the line width to 2. Use RGB triplets to set the face color and edge color for the bars.

```y = [57,91,105,123,131,150,... 170,203,226.5,249,281.4]; figure barh(y,'FaceColor',[0,0.5,0.5],... 'EdgeColor',[0,0,0.9],... 'LineWidth',2)```

Define `x` and `y` as vectors of data.

```x = 1900:10:2000; y = [57,91,105,123,131,150,... 170,203,226.5,249,281.4];```

Create a horizontal bar graph of the data in `y`. Use `x` to specify the bar locations along the y-axis.

```figure barh(x,y)```

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

```load count.dat y = count(1:6,:);```

Create a horizontal bar graph of matrix `y`.

```figure barh(y)```

By default, `barh` groups the bars by row.

Load the data set, `count.dat`, that returns a three-column matrix, `count`. Define `y` as the first four rows of count.

```load count.dat y = count(1:4,:);```

Create a horizontal bar graph of `y` using a dotted line style. Return the three bar series handles. `barh` creates a bar series for each column in `y`.

`h = barh(y,'LineStyle',':');`

Use the handles in `h` to set different property values for each bar series. Change the face color of the first bar series to cyan by setting the `FaceColor` property to `cyan`. Set the face color for the third bar series using an RGB triplet.

Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the `set` function instead.

```h(1).FaceColor = 'cyan'; h(3).FaceColor = [0,0.5,0.5];```

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

```load count.dat y = count(1:6,:);```

Create a horizontal bar graph of `y` and set the basevalue to 25.

```figure barh(y,'BaseValue',25)```