Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

bar, barh - Plot bar graph (vertical and horizontal)

GUI 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

bar(Y)
bar(x,Y)
bar(...,width)
bar(...,'style')
bar(...,'bar_color')
bar(...,'PropertyName',PropertyValue,...)
bar(axes_handle,...)
barh(axes_handle,...)
h = bar(...)
barh(...)
h = barh(...)
hpatches = bar('v6',...)
hpatches = barh('v6',...)

Description

A bar graph displays the values in a vector or matrix as horizontal or vertical bars.

bar(Y) draws one bar for each element in Y. If Y is a matrix, bar 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 scale the x-axis to the highest x-tick on the plot, (a multiple of 10, 100, etc.). If you want the x-axis scale to end exactly at the last bar, you can use the default, and then, for example, type

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

at the MATLAB prompt.

bar(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, bar groups the elements of each row in Y at corresponding locations in x.

bar(...,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.

bar(...,'style') specifies the style of the bars. 'style' is 'grouped' or 'stacked'. 'group' is the default mode of display.

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

bar(...,'PropertyName',PropertyValue,...) set the named property or properties to the specified values. Properties cannot be specified when the hist or histc options are used. See the barseries property descriptions for information on what properties you can set.

bar(axes_handle,...) and barh(axes_handle,...) plot into the axes with the handle axes_handle instead of into the current axes (gca).

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

barh(...) and h = barh(...) create horizontal bars. Y determines the bar length. The vector x is a vector defining the y-axis intervals for horizontal bars. The x-values can be nonmonotonic, but cannot contain duplicate values.

Backward-Compatible Versions

hpatches = bar('v6',...) and hpatches = barh('v6',...) return the handles of patch objects instead of barseries objects for compatibility with MATLAB 6.5 and earlier. Patch objects are also created when the hist and histc options are used, even if the V6 option is not. See patch object properties for a discussion of the properties you can set to control the appearance of these bar graphs.

See

Plot Objects and Backward Compatibility for more information.

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

Note that some barseries object properties set on an individual barseries object set the values for all barseries objects in the graph. See the barseries property descriptions for information on specific properties.

Examples

Single Series of Data

This example plots a bell-shaped curve as a bar graph and sets the colors of the bars to red.

x = -2.9:0.2:2.9;
bar(x,exp(-x.*x),'r')

Bar Graph Options

This example illustrates some bar graph options.

Y = round(rand(5,3)*10);
subplot(2,2,1)
bar(Y,'group')
title 'Group'
subplot(2,2,2) 
bar(Y,'stack')
title 'Stack'
subplot(2,2,3)
barh(Y,'stack')
title 'Stack'
subplot(2,2,4)
bar(Y,1.5)
title 'Width = 1.5'

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 = bar(Y);
set(get(h(1),'BaseLine'),'LineWidth',2,'LineStyle',':')
colormap summer % Change the color scheme

See Also

bar3, ColorSpec, patch, stairs, hist

Area, Bar, and Pie Plots for related functions

Barseries Properties

Bar and Area Graphs for more examples

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS