bar

Draws a bar chart.

Category: Graphics > 2-D and 3-D Plots

Syntax

bar(y)

One bar per element of vector y, at positions 1:n. For a matrix, each row becomes a group and each column a series.

bar(x, y)

Places the bars at the positions in x.

bar(___, Name, Value)

Sets properties such as 'FaceColor' or 'BarWidth'.

b = bar(___)

Also returns the bar objects, one per column of y.

Description

Bars start at the baseline (0 by default) and reach the value of each element. A matrix produces grouped bars, with one color per column; each series is a separate Bar object, which is how legend labels them.

Use barh for horizontal bars, bar3 for 3-D bars and histogram when counting data into bins.

Input arguments

x
Increasing positions of the bars.
y
Heights of the bars: a vector or a matrix.
'FaceColor'
Fill color, as a color name or an RGB triplet.
'BarWidth'
Relative width of each bar, 0.8 by default.

Output arguments

b
Array of Bar objects; read or change their properties with dot syntax.

Example

Single and grouped bars

subplot(1, 2, 1)
bar([3 5 2 8 4])
title('One series')
subplot(1, 2, 2)
bar(1:3, [2 4; 3 1; 5 6])
legend('A', 'B')
title('Two series')

In the app, the figure is drawn below the code.

Notes

A numeric second argument is read as x (not as a width; use 'BarWidth'), and categorical or cell-array x values are not supported.

See also

barh bar3 histogram pie stairs legend