Documentation Center

  • Trials
  • Product Updates

bar

Syntax

  • h = bar(___)

Description

example

bar(Y) draws one bar for each element in Y.

example

bar(x,Y) draws bars for each column in Y at locations specified in x.

example

bar(___,width) sets the relative bar width and controls the separation of bars within a group and can include any of the input arguments in previous syntaxes.

example

bar(___,style) specifies the style of the bars and can include any of the input arguments in previous syntaxes.

example

bar(___,bar_color) displays all bars using the color specified by the single-letter abbreviation of bar_color and can include any of the input arguments in previous syntaxes.

example

bar(___,Name,Value) sets the property names to the specified values and can include any of the input arguments in previous syntaxes.

    Note:   You cannot specify names and values when using hist or histc options.

example

bar(axes_handle,___) plots 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.

Change the colormap to use a different color scheme. Use the colormap function to specify the figure colormap.

Use shading flat to turn off bar edges. Use shading faceted to turn on bar edges. See the shading function for more information.

Examples

expand all

Single Data Series

Use the bar function to plot vector data.

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

X-Axis Tick Labels

Specify the numeric values of the x-axis tick labels.

x = 1900:10:2000;
y = [75.995,91.972,105.711,123.203,131.669,...
     150.697,179.323,203.212,226.505,249.633,281.422];
figure;
bar(x,y);

Plot the y-values at each x-value. Notice that the length(x) and length(y) have to be same.

Bar Width

Set width of each bar to 40 percent of the total space available for each bar.

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

Bar Graph Styles

Create a figure with four subplots containing bar graphs. In each subplot, apply a different style to the bar graph.

Y = round(rand(5,3)*10);

figure;
subplot(2,2,1);
bar(Y,'grouped');
title('Group')

subplot(2,2,2);
bar(Y,'stacked');
title('Stack')

subplot(2,2,3);
bar(Y,'histc');
title('Histc')

subplot(2,2,4);
bar(Y,'hist');
title('Hist')

Specify Axes

Specify the axes for the bar graph by passing an axes handle to bar.

rng(0,'twister'); % initialize random number generator
Y = round(rand(5,3)*10);

figure;
ax = subplot(2,1,2);
bar(ax,Y,'stacked');
title('Lower Subplot')

Bar Color

Specify a red color for the bar graph.

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

Bar Face and Edge Color

Specify green bars and use an RGB color specification for the bar edge color.

y = [75.995,91.972,105.711,123.203,131.669,...
     150.697,179.323,203.212,226.505,249.633,281.422];
figure;
bar(y,'g','EdgeColor',[1,0.5,0.5]);

Bar Graph of 2-D Array

Plot a 6-by-3 array as six groups with three bars each.

c = load('count.dat');
Y = c(1:6,:);
figure;
bar(Y);

Emphasize Subset of Data

Set appearance of one column of data.

Set LineWidth and EdgeColor for the bars representing the second column in the data array y. Pass the handles returned by bar to the set function to set the barseries properties.

c = load('count.dat');
Y = c(1:6,:);

figure
hArray = bar(Y);
set(hArray(2),'LineWidth',2,'EdgeColor','red');

Input Arguments

expand all

x — x-axis intervalsvector | 2-D array

x-axis intervals for vertical bars, specified as a vector or a 2-D array..

The x-values can be nonmonotonic, but cannot contain duplicate values.

Example: x = 1:10;

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Y — Bar lengthsvector | 2-D array

Bar lengths, specified as a vector or a 2-D array.

  • If Y is a vector, then bar creates length(Y) bars. The bar function treats all vectors as column vectors.

  • If Y is a 2–D array, then bar groups the bars produced by the elements in each row.

  • If Y is complex, then bar(Y) plots the imaginary parts of Y versus the index of each entry. If either x or Y is complex, then bar(x,Y) ignores the imaginary parts and plots the real parts of Y versus the real parts of x.

Example: y = [10,8,5,7,3,9,1];

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

width — Bar width0.8 (default) | scalar

Bar width of each bar, specified as a fraction of the total width of a bar and the space between bars. The default of 0.8 means the a bar width is 80% of the space from the previous bar to the next bar, with 10% of that space on each side.

If width is 1, the bars within a group touch one another.

Example: bar(y,0.5);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

style — Bar style, specified as one of the following strings'grouped' (default) | 'stacked' | 'hist' | 'histc'

Bar style, specified by one of these values.

StylePurpose

'grouped'

Displays one group for each row in Y.

  • If Y is an m-by-n matrix, then MATLAB® displays m groups of n vertical bars, where m is the number of rows and n is the number of columns in Y.

  • If Y is a vector of length n, then MATLAB treats Y as a column vector and displays one group of n bars.

'stacked'

Displays one bar for each row in Y.

  • If Y is an m-by-n matrix, then MATLAB displays m bars where each bar height is the sum of the elements in the row. Each bar is multicolored. Colors correspond to distinct elements and show the relative contribution each row element makes to the total sum.

  • If Y is a vector of length n, then MATLAB treats Y as a column vector and displays n bars.

'histc'

Displays the graph in histogram format, in which bars touch one another.

'hist'

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.

Example: bar(Y,'hist')

Data Types: char

bar_color — Bar color'b' | 'r' | 'g' | 'b' | 'c' | 'm' | 'y' | 'k' | 'w'

Bar color, specified as a single-letter abbreviation, assigns the color specified to all bars.

Example: bar(Y,'r')

Data Types: char

axes_handle — Axes handlecurrent axes (default) | axes handle

Axes handle specifying the target axes for the bar graph.

Example: bar(ah,Y);

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: bar(Y,'EdgeColor','g')

For more information see Barseries Property Descriptions.

'BarLayout' — Arrangement of barsgrouped (default) | stacked

Arrangement of bars, specified as the comma-separated pair consisting of 'BarLayout' and one of these values:

  • grouped — Display m groups of n vertical bars, where m is the number of rows and n is the number of columns in the input argument Y. The group contains one bar per column in Y.

  • stacked — Display one bar for each row in the input argument 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.

'BaseValue' — Base line location0 (default) | scalar

Base line location, specified as the comma-separated pair consisting of 'BaseValue' and the value along the y-axis (vertical bars) or x-axis (horizontal bars) at which MATLAB draws the baseline.

'BarWidth' — Relative width of bars0.8 (default) | scalar

Relative bar width, specified as the comma-separated pair consisting of 'BarWidth' and a value relative to width and spacing. BarWidth controls the separation of bars within a group. By default, the bars within a group have a slight separation. A value of 1 makes bars touch with no space. A value greater than 1 makes the bars overlap.

'EdgeColor' — Color of bar edges[0,0,0] (default) | none | ColorSpec

Color of bar edges, specified as the comma-separated pair consisting of 'EdgeColor' and a three-element RGB vector, or one of the MATLAB predefined color specifier names.

'FaceColor' — Color of barsflat (default) | none | ColorSpec

Color of bars, specified as the comma-separated pair consisting of 'FaceColor' and one of these values:

  • flat — Use the figure colormap to determine the color of the bars.

  • ColorSpec — A three-element RGB vector or one of the MATLAB predefined names, specifying a single color for all bars.

  • none — Do not draw faces. Note that MATLAB draws EdgeColor independently of FaceColor.

'LineStyle' — Line style of bar edges– (default) | - | -- | : | -. | none

Line style of bar edges, specified as the comma-separated pair consisting of 'LineStyle' and one of the predefined line style strings.

'LineWidth' — Width of bar edges0.5 points (default) | scalar width specified in points

Width of bar edges, specified as the comma-separated pair consisting of 'LineWidth' and a scalar value representing the line width in points.

Output Arguments

expand all

h — Handle to barseries objectsarray of one or more handles

Handle to barseries objects, returned as an array of one or more handles.

See Also

| | | | |

Was this topic helpful?