barh

Plot bar graph horizontally

Syntax

barh(Y)
barh(X,Y)
barh(...,width)
barh(...,'style')
barh(...,'bar_color')
barh(...,'PropertyName',PropertyValue,...)
barh(axes_handle,...)
h = 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 x-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 default is to assign an appropriate progression of tick values according to the data. If you want the x-axis scale to end exactly at the last bar, set the axis limits as,

set(gca,'XLim',[1 length(Y)])

barh(X,Y) draws a bar for each element in Y at locations specified in x, where X is a vector defining the x-axis intervals for the vertical bars. The x-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.

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. 'style' is 'grouped' or 'stacked'. Default mode of display is 'grouped'.

  • 'grouped' displays m groups of n vertical bars, where m is the number of rows and n 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.

  • '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 x-ticks, rather than making bars span x-ticks 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 series.

barh(...,'bar_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 Series Properties for more information.

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

h = barh(...) returns a vector of bar series handles. When Y is a matrix, barh creates one bar series per column in Y.

Bar Series

Creating a bar graph of an m-by-n matrix creates m groups of n bars. Each bar series comprises a set of bars that have the same color. Use the bar series handle to change properties for all bars in a series.

Examples

expand all

Horizontal Bar Graph of Single Data Series

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 Width for Horizontal Bar Graph

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);

Specify Style for Horizontal Bar Graph

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')

Specify Color for Horizontal Bar Graph

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')

Specify Bar Properties Using Name-Value Pairs

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)

Specify Horizontal Bar Locations

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)

Horizontal Bar Graph of Matrix Data

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.

Specify Different Properties For Each Bar Series

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.

figure
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.

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

Change Baseline Value for Horizontal Bar Graph

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)

See Also

Functions

Properties

Was this topic helpful?