bar - Bar graph - MATLAB (original) (raw)

Syntax

Description

bar([y](#mw%5F52e07ec4-1f73-4807-a9c4-8d1699810fed)) creates a bar graph with one bar for each element in y.

example

bar([x](#mw%5Ff8592ec5-92b3-4f0f-acb0-128a337a575a),[y](#mw%5F52e07ec4-1f73-4807-a9c4-8d1699810fed)) draws the bars at the locations specified by x.

example

bar(___,[width](#bthf%5Fju-width)) sets the relative bar width, which controls the separation of bars within a group. Specifywidth as a scalar value. Use this option with any of the input argument combinations in the previous syntaxes.

example

bar(___,[style](#mw%5F1c5ca259-be55-4bb7-85e3-b409c43fa397)) specifies the style of the bar groups. For example, use 'stacked' to display each group as one multicolored bar.

example

bar(___,[color](#bthf%5Fju%5Fsep%5Fmw%5Fc58e1a8d-49a1-454e-9f85-f7df84158619)) sets the color for all the bars. For example, use 'r' for red bars.

example

bar(___,[Name,Value](#namevaluepairarguments)) specifies properties of the bar graph using one or more name-value pair arguments. Only bar graphs that use the default 'grouped' or 'stacked' style support setting bar properties. Specify the name-value pair arguments after all other input arguments. For a list of properties, see Bar Properties.

example

bar([ax](#bthf%5Fju-ax),___) plots into the axes specified by ax instead of into the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.

example

[b](#mw%5F071725a1-237d-43e5-9e77-7de37d3429ae) = bar(___) returns one or moreBar objects. If y is a vector, thenbar creates one Bar object. Ify is a matrix, then bar returns a Bar object for each series. Use b to set properties of the bars after displaying the bar graph.

example

Examples

collapse all

y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y)

Figure contains an axes object. The axes object contains an object of type bar.

Specify the bar locations along the _x_-axis.

x = 1900:10:2000; y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(x,y)

Figure contains an axes object. The axes object contains an object of type bar.

Since R2023b

Create a string vector x containing the names of four bars. Create a numeric vector y containing the lengths of the bars. Then create a bar chart of x and y.

x = ["Spring" "Summer" "Autumn" "Winter"]; y = [1 2 3 4]; bar(x,y)

Figure contains an axes object. The axes object contains an object of type bar.

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

y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y,0.4)

Figure contains an axes object. The axes object contains an object of type bar.

Display four groups of three bars.

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12]; bar(y)

Figure contains an axes object. The axes object contains 3 objects of type bar.

Display one bar for each row of the matrix. The height of each bar is the sum of the elements in the row.

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12]; bar(y,'stacked')

Figure contains an axes object. The axes object contains 3 objects of type bar.

Create a scalar x and a vector y. Display one stacked bar centered at x=2020. Each section in the stack corresponds to an element of y.

x = 2020; y = [30 50 23]; b = bar(x,y,"stacked");

Figure contains an axes object. The axes object contains 3 objects of type bar.

Adjust the width of the stacked bar. Because the individual bars are stacked, changing the width of one Bar object changes all of them.

Figure contains an axes object. The axes object contains 3 objects of type bar.

Define x as a vector of three year values. Define y as a matrix that contains a combination of negative and positive values. Display the values in a bar graph.

x = [1980 1990 2000]; y = [15 20 -5; 10 -17 21; -10 5 15]; bar(x,y,'stacked')

Figure contains an axes object. The axes object contains 3 objects of type bar.

One way to indicate categories for your bars is to specify X as a categorical array. The bar function uses a sorted list of the categories, so the bars might display in a different order than you expect. To preserve the order, call the reordercats function.

Define X as categorical array, and call the reordercats function to specify the order for the bars. Then define Y as a vector of bar heights and display the bar graph.

X = categorical({'Small','Medium','Large','Extra Large'}); X = reordercats(X,{'Small','Medium','Large','Extra Large'}); Y = [10 21 33 52]; bar(X,Y)

Figure contains an axes object. The axes object contains an object of type bar.

Since R2024b

Create a matrix vals that contains the values of two data sets. Display the values in a bar graph and specify an output argument. Since there are two data sets, bar returns a vector of two Bar objects.

x = [1 2 3]; vals = [2 3 6; 11 23 26]; b = bar(x,vals);

Figure contains an axes object. The axes object contains 2 objects of type bar.

Display the height values, stored in the YData property, at the tips of the first series of bars.

b(1).Labels = b(1).YData;

Figure contains an axes object. The axes object contains 2 objects of type bar.

Then label the second series of bars in the same way.

b(2).Labels = b(2).YData;

Figure contains an axes object. The axes object contains 2 objects of type bar.

As an alternative to using the Labels property to specify bar labels, you can use the text function to create the labels and position them using the XEndPoints and YEndPoints properties.

Create a matrix vals that contains the values of two data sets. Display the values in a bar graph and specify an output argument. Since there are two data sets, bar returns a vector of two Bar objects.

x = [1 2 3]; vals = [2 3 6; 11 23 26]; b = bar(x,vals);

Figure contains an axes object. The axes object contains 2 objects of type bar.

Display the height values at the tips of the first series of bars. Get the coordinates of the tips of the bars by getting the XEndPoints and YEndPoints properties of the first Bar object. Pass those coordinates to the text function, and specify the vertical and horizontal alignment so that the values are centered above the tips of the bars.

xtips1 = b(1).XEndPoints; ytips1 = b(1).YEndPoints; labels1 = string(b(1).YData); text(xtips1,ytips1,labels1,'HorizontalAlignment','center',... 'VerticalAlignment','bottom')

Figure contains an axes object. The axes object contains 5 objects of type bar, text.

Next, display the values above the tips of the second series of bars.

xtips2 = b(2).XEndPoints; ytips2 = b(2).YEndPoints; labels2 = string(b(2).YData); text(xtips2,ytips2,labels2,'HorizontalAlignment','center',... 'VerticalAlignment','bottom')

Figure contains an axes object. The axes object contains 8 objects of type bar, text.

You can display a tiling of bar graphs using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Display a bar graph in the top axes. In the bottom axes, display a stacked bar graph of the same data.

y = [1 2 3; 4 5 6]; tiledlayout(2,1)

% Top bar graph ax1 = nexttile; bar(ax1,y)

% Bottom bar graph ax2 = nexttile; bar(ax2,y,'stacked')

Figure contains 2 axes objects. Axes object 1 contains 3 objects of type bar. Axes object 2 contains 3 objects of type bar.

Create a bar graph using red bars.

y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y,'r')

Figure contains an axes object. The axes object contains an object of type bar.

Set the bar interior color and outline color using RGB triplets. Set the width of the bar outline.

y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)

Figure contains an axes object. The axes object contains an object of type bar.

Control individual bar colors using the CData property of the Bar object.

Create a bar chart and assign the Bar object to a variable. Set the FaceColor property of the Bar object to 'flat' so that the chart uses the colors defined in the CData property. By default, the CData property is prepopulated with a matrix of the default RGB color values. To change a particular color, change the corresponding row in the matrix. For example, change the color of the second bar.

b = bar(rand(10,1)); b.FaceColor = 'flat'; b.CData(2,:) = [.5 0 .5];

Figure contains an axes object. The axes object contains an object of type bar.

Create a bar chart that uses colormap colors by setting the FaceColor property to 'flat'. Then set the CData property for each Bar object to an integer.

y = [1 3 5; 3 2 7; 3 4 2]; b = bar(y,'FaceColor','flat'); for k = 1:size(y,2) b(k).CData = k; end

Figure contains an axes object. The axes object contains 3 objects of type bar.

Since R2023b

Named color palettes provide a convenient way to change the colors of a chart. This example compares a bar chart with three different color palettes.

Create a bar chart of random numbers using the default palette.

Figure contains an axes object. The axes object contains 5 objects of type bar.

Change the color palette to reef by using the colororder function.

Figure contains an axes object. The axes object contains 5 objects of type bar.

Change the color palette to earth.

Figure contains an axes object. The axes object contains 5 objects of type bar.

Create matrix y, where each column is a series of data. Call the bar function to display the data in a bar graph, and specify an output argument. The output is a vector of three Bar objects, where each object corresponds to a different series. This is true whether the bars are grouped or stacked.

y = [10 15 20; 30 35 40; 50 55 62]; b = bar(y);

Figure contains an axes object. The axes object contains 3 objects of type bar.

Make the third series of bars green.

b(3).FaceColor = [.2 .6 .5];

Figure contains an axes object. The axes object contains 3 objects of type bar.

Input Arguments

collapse all

_x_-coordinates, specified as a scalar, vector, matrix, string array, or cell array of character vectors. The values of x do not need to be in order.

If you specify x as a string array or cell array of character vectors, the values must be unique. MATLAB® stores the values as a categorical array, and the bars display in the order you specify.

This table describes some common ways to present your data.

Presentation How to Specify X and Y Example
Display one series of bars. Specify x and y as vectors that are the same length. The values in x must be unique, but the values in y do not need to be unique. x = [1980 1990 2000]; y = [10 20 30]; bar(x,y) Bar chart containing one series of bars. One blue bar is displayed at each location in x.
Display multiple series of bars in groups. Specify either of these combinations: Specify x and y as matrices of equal size. Each column of y corresponds to a series of bars. By default, each series is a different color. To ensure consistent placement of the groups, specify the columns ofx as identical vectors. The values within a column must be unique, even though the columns are repeated.Specify x as a vector of unique values, and specify y as a matrix. The length ofx must equal the length of at least one dimension of y. The other dimension of y contains values for the different series of bars. x = [1980 1980 1980 1990 1990 1990]; y = [2 6 9 11 22 32]; bar(x,y) Orx = [1980 1990]; y = [2 6 9 11 22 32]; bar(x,y) Bar chart containing three series of bars. Each location in x has a group of three bars. The first bar in each group is dark blue, the second bar is dark orange, and the third bar is dark yellow.
Display one group of bars centered at one x value. Specify x as a scalar and y as a vector. x = 1990; y = [10 20 30]; bar(x,y) Bar chart containing one group of bars at the specified x location. The first bar is dark blue, the second bar is dark orange, and the third bar is dark yellow.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration | string (since R2023b) | cell (since R2023b)

_y_-coordinates, specified as a scalar, vector, or matrix. This table describes some common ways to present your data.

Presentation How to Specify X and Y Example
Display one series of bars. Specify x and y as vectors that are the same length. The values in x must be unique, but the values in y do not need to be unique. x = [1980 1990 2000]; y = [10 20 30]; bar(x,y) Bar chart containing one series of bars. One blue bar is displayed at each location in x.
Display multiple series of bars in groups. Specify either of these combinations: Specify x and y as matrices of equal size. Each column of y corresponds to a series of bars. By default, each series is a different color. To ensure consistent placement of the groups, specify the columns ofx as identical vectors. The values within a column must be unique, even though the columns are repeated.Specify x as a vector of unique values, and specify y as a matrix. The length ofx must equal the length of at least one dimension of y. The other dimension of y contains values for the different series of bars. x = [1980 1980 1980 1990 1990 1990]; y = [2 6 9 11 22 32]; bar(x,y) Orx = [1980 1990]; y = [2 6 9 11 22 32]; bar(x,y) Bar chart containing three series of bars. Each location in x has a group of three bars. The first bar in each group is dark blue, the second bar is dark orange, and the third bar is dark yellow.
Display one group of bars centered at one x value. Specify x as a scalar and y as a vector. x = 1990; y = [10 20 30]; bar(x,y) Bar chart containing one group of bars at the specified x location. The first bar is dark blue, the second bar is dark orange, and the third bar is dark yellow.

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

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

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

Example: bar([1 2 3],0.5) creates bars that use 50% of the available space.

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

Group style, specified by one of these values.

Style Result Example
'grouped' Display each group as adjacent bars that are centered around their corresponding x value. Bar chart containing three series of bars. Each location in x has a group of three bars. The first bar in each group is dark blue, the second bar is dark orange, and the third bar is dark yellow.
'stacked' Display each group as one multicolored bar. The length of a bar is the sum of the elements in the group.If y is a vector, then the result is the same as 'grouped'. Bar chart containing three series of bars that are stacked. Each location in x has one bar that has three different colored sections.
'histc' Display the bars in histogram format, in which the bars in a group touch one another. The trailing edge of each group is aligned with the correspondingx value.NoteA better way to display a histogram is to call the histogram function. Bar chart containing four series of bars in the histogram format. Each location in x has a group of four bars. The first bar in each group is dark blue, the second bar light blue, the third bar is green, and the fourth bar is yellow.
'hist' Display the bars in histogram format. Each group is centered at the corresponding x value.NoteA better way to display a histogram is to call the histogram function. Bar chart containing four series of bars in the histogram format. Each location in x has a group of four bars. The first bar in each group is dark blue, the second bar light blue, the third bar is green, and the fourth bar is yellow.

Bar color, specified as one of the options in this table.

Color Name Short Name Appearance
'red' 'r' Sample of the color red
'green' 'g' Sample of the color green
'blue' 'b' Sample of the color blue
'cyan' 'c' Sample of the color cyan
'magenta' 'm' Sample of the color magenta
'yellow' 'y' Sample of the color yellow
'black' 'k' Sample of the color black
'white' 'w' Sample of the color white

Axes object. If you do not specify an axes, then bar uses the current axes for the bar graph.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: bar([10 20 30],'EdgeColor','g') specifies a green outline around the bars.

The Bar properties listed here are only a subset. For a complete list, see Bar Properties.

Note

Color data, specified as one of these values:

By default, when you create a bar chart, the CData property contains a three-column matrix of RGB triplets. You can change the color for a particular bar by changing the corresponding row in the matrix.

This property applies only when the FaceColor orEdgeColor property is set to 'flat'.

Example

Change the color for a particular bar by setting the FaceColor property to 'flat'. Then change the corresponding row in theCData matrix to the new RGB triplet. For example, change the color of the second bar.

b = bar(1:10,'FaceColor','flat'); b.CData(2,:) = [0 0.8 0.8];

Bar chart that has all dark blue bars except the second bar, which is cyan.

Line style of bar outlines, specified as one of the line styles in this table.

Line Style Description Resulting Line
"-" Solid line Sample of solid line
"--" Dashed line Sample of dashed line
":" Dotted line Sample of dotted line
"-." Dash-dotted line Sample of dash-dotted line, with alternating dashes and dots
"none" No line No line

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

Output Arguments

collapse all

Bar objects. Use the elements inb to access and modify properties of a specific Bar object after it has been created. The number ofBar objects depends on the size of y. Ify is a vector, then b is one Bar object. If y is a matrix, thenb is a vector containing a Bar object for each series in y.

More About

collapse all

A series consists of the bars at all locations in X for a particular set of data. By default, each series of bars is indicated by a different color.

A group consists of all the bars at a particular location inX.

Extended Capabilities

expand all

The bar function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

When you create a bar chart, you can specify the bar labels as a string vector or cell array of character vectors. The bar tick labels appear in the order you specify them.