Skip to Main Content Skip to Search
Product Documentation

barh - Plot bar graph horizontally

Alternatives

To graph selected variables, use the Plot Selector in the Workspace Browser, or use the Figure Palette Plot Catalog. Manipulate graphs in plot edit mode with the Property Editor. For details, see Plotting Tools — Interactive Plotting in the MATLAB Graphics documentation and Creating Plots from the Workspace Browser in the MATLAB Desktop Tools and Development Environment documentation.

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 ia 'grouped'.

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 speciy properties when hist or histc options are used. See the barseries property descriptions for information on what properties you can set.

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 handles to barseries graphics objects, one for each created. When Y is a matrix, barh creates one barseries graphics object per column in Y.

Barseries Objects

Creating a bar graph of an m-by-n matrix creates m groups of n barseries objects. Each barseries object contains the data for corresponding x values of each bar group (as indicated by the coloring of the bars).

Examples

Single Series of Data

This example shows how to plot vector data using barh .

y = [75.995 91.972 105.711 123.203 131.669 ...
     150.697 179.323 203.212 226.505 249.633 281.422];
figure; barh(y);

Specifying width While Passing Single Variable.

This example shows how to specify width for each bar in the graph. You can specify one or both data inputs.

figure; barh(y,0.4);

Specifying style

This example shows you how to specify Style for bar graph.

figure;
subplot(2,2,1); barh(y,'grouped');
subplot(2,2,2); barh(y,'stacked');
subplot(2,2,3); barh(y,'hist');
subplot(2,2,4); barh(y,'histc');

Specifying color

This example shows how to specify color for the graph.

figure; barh(y,'r');

Specifying Name-Value Pairs

You can instead specify the rgb color instead of using the predefined ones. For example,

figure; barh(y,'g','EdgeColor',[1 0.5 0.5]);

Passing Two Variables

This example shows how to plot vector data with two inputs x and y using barh .

x = [1900:10:2000];
figure; barh(x,y);

Setting Axis Scale

The default is to assign an appropriate progression of tick values according to the data. If you want the y-axis scale to end exactly at the last bar, you can change the axes YLim property, which sets the limits of the y-axis. See axes for more information.

x = [1900:10:2000];
figure; barh(x,y);
set(gca,'YLim',[1 max(y)]);

Passing an Expression

You can also pass the expression with the barh function.

a = -2.9:0.2:2.9;
barh(a,exp(-a.*a),'r')

Plotting Matrix Data

This example shows how to plot matrix data using barh.

load count.dat;
yMat = count(1:6,:);
figure; barh(yMat);

Setting Properties with Multiobject Graphs

This example shows how to set LineWidth and LineStyle for three barseries objects using the handle returned by barh.

hMulti = barh(yMat);
set(hMulti,'LineWidth', 2, 'LineStyle',':');

Setting Properties with Multiobject Graphs

This example creates a graph that displays three groups of bars and contains five barseries objects. Since all barseries objects in a graph share the same baseline, you can set values using any barseries object's BaseLine property. This example uses the first handle returned in h.

Y = randn(3,5);
h = barh(Y);
set(get(h(1),'BaseLine'),'LineWidth',2,'LineStyle',':')
colormap summer % Change the color scheme

Specifying colormap assigns a specific color in the map spectrum to each handle object in the group resulting in one color for each object group.

See Also

bar | bar3 | bar3h | Barseries Properties | ColorSpec | hist | stairs

How To

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS